if ((oldfd = win32_dup(stdfd)) == -1)
goto cleanup;
+ /* save the old std handle (this needs to happen before the
+ * dup2(), since that might call SetStdHandle() too) */
+ OP_REFCNT_LOCK;
+ lock_held = 1;
+ old_h = GetStdHandle(nhandle);
+
/* make stdfd go to child end of pipe (implicitly closes stdfd) */
/* stdfd will be inherited by the child */
if (win32_dup2(p[child], stdfd) == -1)
/* close the child end in parent */
win32_close(p[child]);
- /* save the old std handle, and set the std handle */
- OP_REFCNT_LOCK;
- lock_held = 1;
- old_h = GetStdHandle(nhandle);
+ /* set the new std handle (in case dup2() above didn't) */
SetStdHandle(nhandle, (HANDLE)_get_osfhandle(stdfd));
/* start the child */
if ((childpid = do_spawn_nowait((char*)command)) == -1)
goto cleanup;
- /* restore the old std handle */
+ /* revert stdfd to whatever it was before */
+ if (win32_dup2(oldfd, stdfd) == -1)
+ goto cleanup;
+
+ /* restore the old std handle (this needs to happen after the
+ * dup2(), since that might call SetStdHandle() too */
if (lock_held) {
SetStdHandle(nhandle, old_h);
OP_REFCNT_UNLOCK;
lock_held = 0;
}
- /* revert stdfd to whatever it was before */
- if (win32_dup2(oldfd, stdfd) == -1)
- goto cleanup;
-
/* close saved handle */
win32_close(oldfd);