Option to launch processes as children
Discussion
- What aspect of the language would you like to see improved?
Process.run to have an option to kill the child if the primary process exits
- What are the reasons?
This simplifies cleaning up worker processes on application exit and provides a method to ensure processes are cleaned up if a process crashes or is forcefully killed
- Include practical examples to illustrate your points.
Worker processes in web applications I personally have processes that launch ffmpeg for certain tasks and find it challenging to ensure they are always cleaned up
- Optionally add one (or more) proposals to improve the current situation.
Linux has the prctl system call with the PR_SET_PDEATHSIG option on linux would do the job (fork -> prctl call -> replace with child process)
BSD has procctl with PROC_PDEATHSIG_CTL
Windows has CreateJobObject to assign workers to a pool which will be cleaned up on exit
This issue has been mentioned on Crystal Forum. There might be relevant details there:
https://forum.crystal-lang.org/t/child-workers-that-are-automatically-reaped/7047/1
Process already uses CreateJobObjectW on Windows to support IOCP, I don't know if it achieves what you want yet
That's interesting, it's probably very simple to achieve this on Windows then
This is the cpp code to have children terminate when a job object is closed
// Set the job object to terminate all associated processes when the job is closed
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobInfo = {0};
jobInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if (!SetInformationJobObject(job, JobObjectExtendedLimitInformation, &jobInfo, sizeof(jobInfo))) {
std::cerr << "SetInformationJobObject failed with error: " << GetLastError() << std::endl;
CloseHandle(job);
return;
}
https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_limit_information
Are you aware of any other process spawn APIs exposing a portable feature for automatically terminating a child process when the parent dies? If we were to implement this, it must be optional because you might not want your children to die.
It doesn't look too hard to implement, but I'm not sure if we can really on this being available across the board.