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

common/CScriptObj: use posix_spawn() instead of vfork()/execlp() #1368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MaxKellermann
Copy link

Using vfork() directly is extremely fragile, and posix_spawn() is easier to use. This implements a TODO comment.

Using vfork() directly is extremely fragile, and posix_spawn() is
easier to use.  This implements a TODO comment.
@raydienull raydienull requested a review from cbnolok January 16, 2025 08:03
@cbnolok
Copy link
Contributor

cbnolok commented Jan 21, 2025

Thanks for the PR! Did you manage to test it?

@MaxKellermann
Copy link
Author

No, I admit I havn't tested it because I don't know how to call it. I changed this only because Sphere wouldn't compile with vfork():

FAILED: CMakeFiles/spheresvr_release.dir/src/common/CScriptObj.cpp.o 
[...]
In file included from Debug/CMakeFiles/spheresvr_release.dir/cmake_pch.hxx:9,
                 from <command-line>:
src/common/CScriptObj.cpp: In member function ‘virtual bool CScriptObj::r_WriteVal(lpctstr, CSString&, CTextConsole*, bool, bool)’:
src/common/CScriptObj.cpp:427:9: error: variable ‘inLocalBlock’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Werror=clobbered]
  427 |         EXC_TRY("WriteVal-Ref");
      |         ^~~~~~~
src/common/CScriptObj.cpp:427:9: error: variable ‘inLocalBlockCnt’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Werror=clobbered]
  427 |         EXC_TRY("WriteVal-Ref");
      |         ^~~~~~~
cc1plus: all warnings being treated as errors

vfork() is indeed a minefield and nearly impossible to use safely without resorting to assembly, because you never know what the C++ compiler generates. But what Sphere does here is illegal, not even counting for compiler quirks. I wonder how this could ever have worked at all.
So something had to be done.

I thought the function was buggy because it left zombie processes, but then I saw the SIGCHLD handler which calls waitpid() from within the signal handler. I didn't know this was legal (maybe it isn't). But this can obviously break the waitpid() call in CScriptObj (maybe not if Sphere takes care to adjust the signal block mask before calling CScriptObj, but I guess it does not).
So the function is indeed buggy after all, but differently buggy than I thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants