Index: /trunk/src/kash/shfile.c
===================================================================
--- /trunk/src/kash/shfile.c	(revision 3443)
+++ /trunk/src/kash/shfile.c	(revision 3444)
@@ -240,4 +240,6 @@
     if (fdMin >= SHFILE_MAX)
     {
+        TRACE2((NULL, "shfile_insert: fdMin=%d is out of bounds; native=%p\n", fdMin, native));
+        shfile_native_close(native, oflags);
         errno = EMFILE;
         return -1;
@@ -247,4 +249,5 @@
     {
         int e = errno;
+        TRACE2((NULL, "shfile_insert: F_SETFD failed %d; native=%p\n", e, native));
         close((int)native);
         errno = e;
@@ -277,4 +280,5 @@
         pfdtab->tab[fd].shflags = shflags;
         pfdtab->tab[fd].native = native;
+        TRACE2((NULL, "shfile_insert: #%d: native=%p oflags=%#x shflags=%#x\n", fd, native, oflags, shflags));
     }
     else
@@ -847,8 +851,10 @@
                     else
                     {
-# ifdef SH_FORKED_MODE
+# if K_OS == K_OS_WINDOWS
+#  ifdef SH_FORKED_MODE
                         KBOOL const cox = !!(src->shflags & SHFILE_FLAGS_CLOSE_ON_EXEC);
-# else
-                        KBOOL const cox = !!(src->shflags & SHFILE_FLAGS_CLOSE_ON_EXEC); /// @todo K_TRUE
+#  else
+                        KBOOL const cox = K_TRUE;
+#  endif
 # endif
                         *dst = *src;
@@ -859,5 +865,5 @@
                                             (HANDLE *)&dst->native,
                                             0,
-                                            cox ? FALSE : TRUE /* bInheritHandle */,
+                                            FALSE /* bInheritHandle */,
                                             DUPLICATE_SAME_ACCESS))
                             TRACE2((NULL, "shfile_init: %d (%#x, %#x) %p (was %p)\n",
@@ -1054,5 +1060,5 @@
  * @returns Pointer to CRT data if prepare is 1, NULL if prepare is 0.
  * @param   pfdtab  The file descriptor table.
- * @param   prepare Which call, 1 if before and 0 if after.
+ * @param   prepare Which call, 1 if before, 0 if after and success, -1 if after on failure.
  * @param   sizep   Where to store the size of the returned data.
  * @param   hndls   Where to store the three standard handles.
@@ -1074,5 +1080,5 @@
         count--;
 
-    if (prepare)
+    if (prepare > 0)
     {
         size_t      cbData = sizeof(int) + count * (1 + sizeof(HANDLE));
@@ -1127,13 +1133,27 @@
     else
     {
+        shfile *file = pfdtab->tab;
         assert(!hndls);
         assert(!sizep);
         i = count;
-        while (i-- > 0)
-        {
-            if (    pfdtab->tab[i].fd == i
-                &&  !(pfdtab->tab[i].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
-                shfile_set_inherit_win(&pfdtab->tab[i], 0);
-        }
+        if (prepare == 0)
+            for (i = 0; i < count; i++, file++)
+            {
+                if (   file->fd == i
+                    && !(file->shflags & SHFILE_FLAGS_TRACE))
+                {
+                    shfile_native_close(file->native, file->oflags);
+
+                    file->fd = -1;
+                    file->oflags = 0;
+                    file->shflags = 0;
+                    file->native = -1;
+                }
+            }
+        else
+            for (i = 0; i < count; i++, file++)
+                if (    file->fd == i
+                    &&  !(file->shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
+                    shfile_set_inherit_win(file, 0);
         pvRet = NULL;
     }
@@ -1945,4 +1965,29 @@
 
     TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
+    return rc;
+}
+
+/**
+ * Sets the SHFILE_FLAGS_TRACE flag.
+ */
+int shfile_set_trace(shfdtab *pfdtab, int fd)
+{
+    int         rc;
+#ifdef SHFILE_IN_USE
+    shmtxtmp    tmp;
+    shfile     *file = shfile_get(pfdtab, fd, &tmp);
+    if (file)
+    {
+        file->shflags |= SHFILE_FLAGS_TRACE;
+        shfile_put(pfdtab, file, &tmp);
+        rc = 0;
+    }
+    else
+        rc = -1;
+#else
+    rc = 0;
+#endif
+
+    TRACE2((NULL, "shfile_set_trace(%d) -> %d\n", fd, rc));
     return rc;
 }
Index: /trunk/src/kash/shfile.h
===================================================================
--- /trunk/src/kash/shfile.h	(revision 3443)
+++ /trunk/src/kash/shfile.h	(revision 3444)
@@ -112,4 +112,5 @@
  */
 #define SHFILE_FLAGS_CLOSE_ON_EXEC      0x0001
+#define SHFILE_FLAGS_TRACE              0x0002  /**< The 'trace' file, keep open after execve. */
 #define SHFILE_FLAGS_TYPE_MASK          0x00f0
 #define SHFILE_FLAGS_FILE               0x0000
@@ -154,4 +155,5 @@
 int shfile_isatty(shfdtab *, int);
 int shfile_cloexec(shfdtab *, int, int);
+int shfile_set_trace(shfdtab *, int);
 int shfile_ioctl(shfdtab *, int, unsigned long, void *);
 #if defined(_MSC_VER) || defined(__OS2__)
Index: /trunk/src/kash/shinstance.c
===================================================================
--- /trunk/src/kash/shinstance.c	(revision 3443)
+++ /trunk/src/kash/shinstance.c	(revision 3444)
@@ -44,4 +44,5 @@
 #include "error.h"
 #include "memalloc.h"
+#include "redir.h"
 #include "shell.h"
 #include "trap.h"
@@ -1841,5 +1842,5 @@
                            &ProcInfo);
 
-        shfile_exec_win(&psh->fdtab, 0 /* done */, NULL, NULL);
+        shfile_exec_win(&psh->fdtab, rc ? 0 /* done */ : -1 /* done but failed */, NULL, NULL);
 #ifndef SH_FORKED_MODE
         shmtx_leave(&g_sh_exec_mtx, &tmp);
Index: /trunk/src/kash/show.c
===================================================================
--- /trunk/src/kash/show.c	(revision 3443)
+++ /trunk/src/kash/show.c	(revision 3444)
@@ -522,4 +522,5 @@
 		}
 		shfile_cloexec(&psh->fdtab, psh->tracefd, 1 /* close it */);
+		shfile_set_trace(&psh->fdtab, psh->tracefd);
 	}
 	if (psh->tracefd == -1) {
