New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected interference between create-process(fork) and at-exit #7209
Comments
Comment author: bobot Upload the archived files for easier reading. |
Comment author: bobot 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: |
Comment author: @diml A simple solution is to call |
Comment author: lcorrenson Great! |
Comment author: bobot For 4.03 the proposed fix of Jeremy Dimino is great. For a later version I'm wondering if |
Comment author: lcorrenson It'd be better to continue the discussion on the pull request. |
Original bug ID: 7209
Reporter: lcorrenson
Assigned to: @diml
Status: closed (set by @xavierleroy on 2017-09-24T15:32:12Z)
Resolution: fixed
Priority: normal
Severity: minor
Version: 4.02.3
Fixed in version: 4.03.0+dev / +beta1
Category: standard library
Monitored by: bobot @dbuenzli
Bug description
Originally, we discover the « bug » when creating a process with a non-existing command :
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 reproduce
See 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 information
As a work-around, we simply use:
let mypid = Unix.getpid ()
let at_exit f = Persasives.at_exit
(fun () -> if Unix.getpid () = mypid then f ())
File attachments
The text was updated successfully, but these errors were encountered: