Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0001784OCamlOCaml generalpublic2003-07-30 07:192011-05-29 12:19
Reporteradministrator 
Assigned Toxleroy 
PrioritynormalSeverityfeatureReproducibilityalways
StatusclosedResolutionfixed 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version3.12.0+dev 
Summary0001784: Segfault in threads library
DescriptionFull_Name: Annan Harley
Version: 3.06
OS: Linux 2.4
Submission from: h24-78-76-99.vc.shawcable.net (24.78.76.99)


I can regularly reproduce segfaults by using pthread_create in a C function,
then calling an OCaml closure set up in a different thread. Code to reproduce
the condition follows.

***************** oops.c
#include <pthread.h>
#include <unistd.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/callback.h>

static value * f = 0;
static value * v = 0;
static pthread_t p = 0;
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t c = PTHREAD_COND_INITIALIZER;

/* so we need a function to call from OCaml that sets the callback,
   a function that runs the callback, and a function (called from OCaml)
   that starts the thread that runs the function that runs the callback.

   whee. */

value im_setting_the_callback(value no_val) {
  CAMLparam1 (no_val);

  f = caml_named_value("segfault_function");
  v = caml_named_value("segfault_function_val");

  CAMLreturn(Val_unit);
}

void* look_at_me_segfault(void *arg) {
  CAMLparam0 ();

  pthread_mutex_lock(&m);
  pthread_cond_signal(&c);
  pthread_mutex_unlock(&m);

  callback(*f, *v);

  CAMLreturn0;
}

value segfault_thread_in_C(value v) {
  CAMLparam1 (v);

  pthread_mutex_lock(&m);

  pthread_create(&p, NULL, &look_at_me_segfault, NULL);

  pthread_cond_wait(&c, &m);
  pthread_mutex_unlock(&m);

 CAMLreturn(Val_unit);
}
**********

********** oops.ml
external set_callback : unit -> unit = "im_setting_the_callback"
external run_callback_from_C_thread : unit -> unit = "segfault_thread_in_C"

let look_at_me_segfault s = print_string s; flush Pervasives.stdout

let main () =
  Callback.register "segfault_function" look_at_me_segfault;
  Callback.register "segfault_function_val" "oops, i'm segfaulting\n";

  set_callback ();
  Thread.create run_callback_from_C_thread ();

  Unix.sleep 2;;

main ();;
**********

build with
gcc -c -g -I /usr/local/lib/ocaml -o oops_c.o oops.c
ocamlopt -o oops -thread -cclib -lpthread unix.cmxa threads.cmxa oops_c.o
oops.ml

Thanks much.

TagsNo tags attached.
Attached Files

- Relationships
related to 0004702closedxleroy C-threads and callbacks 

-  Notes
(0000160)
administrator (administrator)
2003-08-20 11:49

Currently, cannot callback Caml from a system thread that it didn't create.
Need to implement an API for threaded callbacks in the future.
(0005392)
xleroy (administrator)
2010-04-27 10:01

Added API to register threads created from C with the Caml run-time system. Will go in 3.12.0.

- Issue History
Date Modified Username Field Change
2005-11-18 10:13 administrator New Issue
2009-08-20 16:28 xclerc Relationship added related to 0004702
2010-04-27 10:01 xleroy Note Added: 0005392
2010-04-27 10:01 xleroy Assigned To => xleroy
2010-04-27 10:01 xleroy Status acknowledged => resolved
2010-04-27 10:01 xleroy Resolution open => fixed
2010-04-27 10:01 xleroy Fixed in Version => 3.12.0+dev
2010-04-27 10:01 xleroy Description Updated
2011-05-29 12:19 xleroy Status resolved => closed


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker