Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007209OCamlstandard librarypublic2016-04-04 10:202017-09-24 17:32
Reporterlcorrenson 
Assigned Todim 
PrioritynormalSeverityminorReproducibilityalways
StatusclosedResolutionfixed 
PlatformOSOS Version
Product Version4.02.3 
Target VersionFixed in Version4.03.0+dev / +beta1 
Summary0007209: Unexpected interference between create-process(fork) and at-exit
DescriptionOriginally, we discover the « bug » when creating a process with a non-existing command :
 - Pervasive.at_exit some job
 - Unix.create_process with a wrong command
 - The job is executed twice

Diagnosis : Unix.create_process forks the original process, which is an ocaml runtime with Pervasive.at_exit registered job(s). Then, Unix.create_process fails without cleaning the duplicated runtime, hence the job is executed in both processes. When Unix.create_process succeed, the registered jobs are executed once as expected.

Less surprising, the « bug » is also visible when using Unix.fork -- is it a bug or a feature ?
A work around is to check within jobs registered via Pervasives.at_exit whether the pid has changed or not.
Though, we would suggest that any forking operation in the standard library starts by cleaning the daemons in the child processes.

Steps To ReproduceSee attached files proc.ml and fork.ml :

$ ocamlbuild -package unix proc.native && ./proc.native ocamlc # Existing process (normal behavior)
$ ocamlbuild -package unix proc.native && ./proc.native wrong # Wrong process (unexpected)
$ ocamlbuild -package unix fork.native && ./fork.native # Fork case (non intuitive)
Additional InformationAs a work-around, we simply use:

let mypid = Unix.getpid ()
let at_exit f = Persasives.at_exit
   (fun () -> if Unix.getpid () = mypid then f ())
TagsNo tags attached.
Attached Fileszip file icon bug.zip [^] (803 bytes) 2016-04-04 10:20
? file icon fork.ml [^] (326 bytes) 2016-04-04 11:18 [Show Content]
? file icon proc.ml [^] (573 bytes) 2016-04-04 11:18 [Show Content]

- Relationships

-  Notes
(0015663)
bobot (reporter)
2016-04-04 11:19

Upload the archived files for easier reading.
(0015664)
bobot (reporter)
2016-04-04 11:24

For Unix.create_process, I agree that registered functions should not be run. But for generic fork I disagree since it is completely the inverse of the POSIX C atexit: `When a child process is created via fork(2), it inherits copies of its parent's registrations. Upon a successful call to one of the exec(3) functions, all registrations are removed.`
(0015665)
dim (developer)
2016-04-04 11:30

A simple solution is to call `sys_exit` (defined in pervasives.ml but not exported) instead of `exit` in `Unix.create_process`, to avoid running `at_exit` handlers. I did this a while ago in `Lwt_process` but forgot to upstream it. I'll do a patch
(0015667)
dim (developer)
2016-04-04 11:54

Fix proposed in https://github.com/ocaml/ocaml/pull/532 [^]
(0015668)
lcorrenson (reporter)
2016-04-04 12:21

Great!
(0015669)
bobot (reporter)
2016-04-04 13:34

For 4.03 the proposed fix of Jeremy Dimino is great. For a later version I'm wondering if `caml_sys_exit` is the best since it prints gc statistics and gc instrumented time. It is just noise for these functions that does no allocation. A direct call to the C `exit` function would be better.
(0015670)
lcorrenson (reporter)
2016-04-04 14:10

It'd be better to continue the discussion on the pull request.
(0015673)
dim (developer)
2016-04-04 16:49

Fix is now merged in 4.03. @bobot: indeed, or maybe even `_exit` which skip the C `atexit` handlers as well

- Issue History
Date Modified Username Field Change
2016-04-04 10:20 lcorrenson New Issue
2016-04-04 10:20 lcorrenson File Added: bug.zip
2016-04-04 11:18 bobot File Added: fork.ml
2016-04-04 11:18 bobot File Added: proc.ml
2016-04-04 11:19 bobot Note Added: 0015663
2016-04-04 11:24 bobot Note Added: 0015664
2016-04-04 11:28 dim Assigned To => dim
2016-04-04 11:28 dim Status new => assigned
2016-04-04 11:30 dim Note Added: 0015665
2016-04-04 11:54 dim Note Added: 0015667
2016-04-04 12:21 lcorrenson Note Added: 0015668
2016-04-04 13:34 bobot Note Added: 0015669
2016-04-04 14:10 lcorrenson Note Added: 0015670
2016-04-04 16:49 dim Note Added: 0015673
2016-04-04 16:49 dim Status assigned => resolved
2016-04-04 16:49 dim Fixed in Version => 4.03.0+dev / +beta1
2016-04-04 16:49 dim Resolution open => fixed
2017-02-23 16:43 doligez Category OCaml standard library => standard library
2017-09-24 17:32 xleroy Status resolved => closed


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker