Issue 13753 - src/std/process.d: _spawnvp error handling is broken
Summary: src/std/process.d: _spawnvp error handling is broken
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-11-19 21:33 UTC by Danny Milosavljevic
Modified: 2024-12-01 16:22 UTC (History)
1 user (show)

See Also:


Attachments
Tests whether spawnvp magically forks processes for the caller (348 bytes, text/x-dsrc)
2014-11-19 21:36 UTC, Danny Milosavljevic
Details
Patch to make _spawnvp less bad (2.38 KB, patch)
2014-11-19 21:56 UTC, Danny Milosavljevic
Details | Diff

Note You need to log in before you can comment on or make changes to this issue.
Description Danny Milosavljevic 2014-11-19 21:33:14 UTC
The _spawnvp in src/std/process.d is broken.

First, if the child process (say it has pid B) fails to execvp, it's a bad idea to then throw an Exception. The entire point of spawnvp in general is to hide the fact that the current process A was forked off. But now the Exception propagates through process B, so certainly the caller will notice that something is off (the caller is suddenly inside another process than he started out in). 

Later on, the waitpid result is not checked. It is possible for waitpid to return (-1). In that case, errno contains the error code and "status" contains garbage, which is then compared against.

Also, all Posix system calls can return (-1) and errno = EINTR (see <http://www.jwz.org/doc/worse-is-better.html>, search for "PC loser-ing") to indicate that while the user process asked for action S to be performed, really it should be checking and doing some other action T before.

So for the latter there really should be some global delegate that is called on EINTR which decides whether to do anything about it, possibly terminating the loop (or not, it depends). 

This is not specific to process.d but all functions that do system calls should call this. Even std.stdio.File functions should do this.

Also, it throws an Exception (literally that) using strerror_r to build it instead of just using ErrnoException. Why?
Comment 1 Danny Milosavljevic 2014-11-19 21:36:22 UTC
Created attachment 1454 [details]
Tests whether spawnvp magically forks processes for the caller

Tests whether spawnvp magically switches processes. Try with version(Posix) implementation of spawnvp.
Comment 2 Danny Milosavljevic 2014-11-19 21:56:03 UTC
Created attachment 1455 [details]
Patch to make _spawnvp less bad

checks waitpid() return value, does not magically put the caller into a new process. Does not properly handle EINTR.
Comment 4 Danny Milosavljevic 2014-11-25 18:25:13 UTC
Also, both the OSX and the Posix version of browse in the same file are broken in the same way...
Comment 5 berni44 2019-12-10 10:27:22 UTC
spawnvp seems only to exist for windows now. But I did not check if this has been the case in 2014 too.

Tried to adapt the test for "browse":

```
import std.process;
import core.thread;

int main() {
        auto pidBefore = getpid();
        try {
                browse("DOESNOTEXIST");
                assert(false);
        } catch(Exception e) {
                auto pidAfterwards = getpid();
                assert(pidBefore == pidAfterwards); // make sure we are still in the same process
        }
        return 0;
}
```

This produces an Assertion failure (POSIX). And the process seems still to be running after the main process has stopped.
Comment 6 dlangBugzillaToGithub 2024-12-01 16:22:59 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/10099

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB