Skip to content
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

Closed
vicuna opened this Issue Apr 4, 2016 · 8 comments

Comments

Projects
None yet
2 participants
@vicuna
Copy link
Collaborator

vicuna commented Apr 4, 2016

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 :

  • 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 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

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Apr 4, 2016

Comment author: bobot

Upload the archived files for easier reading.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Apr 4, 2016

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: 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.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Apr 4, 2016

Comment author: @diml

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

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Apr 4, 2016

Comment author: @diml

Fix proposed in #532

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Apr 4, 2016

Comment author: lcorrenson

Great!

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Apr 4, 2016

Comment author: bobot

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.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Apr 4, 2016

Comment author: lcorrenson

It'd be better to continue the discussion on the pull request.

@vicuna

This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Apr 4, 2016

Comment author: @diml

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

@vicuna vicuna closed this Sep 24, 2017

@vicuna vicuna added the stdlib label Mar 14, 2019

@vicuna vicuna added the bug label Mar 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.