Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007459OCamlotherlibspublic2017-01-13 09:252017-02-19 17:51
Reporterkayceesrk 
Assigned To 
PrioritynormalSeverityminorReproducibilityalways
StatusresolvedResolutionfixed 
PlatformOSUbuntuOS Version16.04
Product Version4.04.0 
Target Version4.05.0 +dev/beta1/beta2/beta3/rc1Fixed in Version4.05.0 +dev/beta1/beta2/beta3/rc1 
Summary0007459: Systhreads may be not initialized with multi-threaded callbacks
DescriptionThe systhreads scheduler is initialized (by caml_thread_initialize) when linking against the threads library and the OCaml code refers to the thread module. The initialization ensures that acquiring the runtime acquires the runtime master lock. If the OCaml code does not refer to the Thread module, the initialization code is never run, and the master lock is not initialize.

A possible fix is to expose `caml_thread_initialize` and require any multi-threaded C program calling into OCaml call this function after calling `caml_main`. Initialization is already protected against duplicate initializations. Other C threads unknown to the runtime still invoke `caml_c_thread_register`.
Steps To ReproduceThe test case has 2 pthreads calling into OCaml code. Since the access is not protected by the runtime lock, errors are seen.

Untar the attachment and run `make`. Run `./app.native`. You should see:

....
[0x7f610fe8a700] expected=10 seen=0
[0x7f610fe8a700] expected=10 seen=0
[0x7f610fe8a700] expected=10 seen=0
[0x7f6111143700] expected=10 seen=1
[0x7f610fe8a700] expected=10 seen=1
[0x7f610fe8a700] expected=10 seen=1
[0x7f610f689700] expected=10 seen=0
[0x7f6111143700] expected=10 seen=1
[0x7f610fe8a700] expected=10 seen=0
[0x7f610fe8a700] expected=10 seen=1
[0x7f6111143700] expected=10 seen=0
[0x7f610fe8a700] expected=10 seen=1
[0x7f610fe8a700] expected=10 seen=1
[0x7f6111143700] expected=10 seen=1
....

which indicates errors. This can be "fixed" by uncommenting the first line in `caml_stubs.ml`:

```
let _ = Threads.self
```

if you recompile and run the program again, none of the errors are seen.
TagsNo tags attached.
Attached Filesgz file icon ocaml-thread-init.tar.gz [^] (946 bytes) 2017-01-13 09:25

- Relationships

-  Notes
(0017157)
xleroy (administrator)
2017-01-13 15:39

yes, we could expose caml_thread_initialize and let the C part of the application call it at the right time. I wonder how many other libraries (from the core OCaml distribution or third-party) have a similar problem that would have to be fixed similarly by exposing a C initialization function.

A possible alternative would be to mark some OCaml compilation units as initialization code that must always be run even if their definitions are not referenced. Basically, it's the -linkall option applied to individual compilation units.

Speaking of -linkall, in 4.03 the systhreads library is built with -linkall for Win32 but not for Unix, and in the current OCaml trunk, the bytecode version of the systhreads library is built with -linkall, but not the native-code version. This needs cleaning up. But the point remain that the versions of the systhreads library built with -linkall very probably do not exhibit the problem shown here.
(0017158)
kayceesrk (reporter)
2017-01-13 16:31

It seems to be the case that native-code version in trunk is also compiled with -linkall: https://github.com/ocaml/ocaml/blob/trunk/otherlibs/systhreads/Makefile#L82-L83 [^]
(0017159)
xleroy (administrator)
2017-01-13 16:47

Indeed I misread the Makefile in the current trunk. So, maybe the problem is gone with the trunk version of systhreads?
(0017160)
kayceesrk (reporter)
2017-01-13 18:10

Yes. I can confirm that the problem is gone with trunk.
(0017161)
xleroy (administrator)
2017-01-13 18:51

Thanks for checking. So, the problem is fixed, even though the fix leaves me slightly unsatisfied.

See also: https://github.com/ocaml/ocaml/pull/1009 [^] for a modest improvement to -linkall.
(0017185)
kayceesrk (reporter)
2017-01-19 11:54

This issue should be closed since the problem has been fixed in trunk.

- Issue History
Date Modified Username Field Change
2017-01-13 09:25 kayceesrk New Issue
2017-01-13 09:25 kayceesrk File Added: ocaml-thread-init.tar.gz
2017-01-13 15:39 xleroy Note Added: 0017157
2017-01-13 15:42 xleroy Status new => acknowledged
2017-01-13 16:31 kayceesrk Note Added: 0017158
2017-01-13 16:47 xleroy Note Added: 0017159
2017-01-13 18:10 kayceesrk Note Added: 0017160
2017-01-13 18:51 xleroy Note Added: 0017161
2017-01-19 11:54 kayceesrk Note Added: 0017185
2017-02-19 17:51 xleroy Status acknowledged => resolved
2017-02-19 17:51 xleroy Resolution open => fixed
2017-02-19 17:51 xleroy Fixed in Version => 4.05.0 +dev/beta1/beta2/beta3/rc1
2017-02-19 17:51 xleroy Target Version => 4.05.0 +dev/beta1/beta2/beta3/rc1
2017-02-23 16:42 doligez Category OCaml otherlibs => otherlibs


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker