@@ -44,7 +44,7 @@ internal actual suspend fun executeCommand(
4444 }
4545
4646 // NOTE: CreateFileW returns HANDLE? in Kotlin/Native bindings
47- val hOut: HANDLE ? = CreateFileW (
47+ var hOut: HANDLE ? = CreateFileW (
4848 /* lpFileName = */ outPath,
4949 /* dwDesiredAccess = */ GENERIC_WRITE .toUInt(),
5050 /* dwShareMode = */ (FILE_SHARE_READ or FILE_SHARE_WRITE ).toUInt(),
@@ -95,7 +95,10 @@ internal actual suspend fun executeCommand(
9595 val waitRc: UInt = WaitForSingleObject (pi.hProcess, timeoutMillis.toUInt())
9696 if (waitRc == WAIT_TIMEOUT .toUInt()) {
9797 TerminateProcess (pi.hProcess, 124u )
98- CloseHandle (hOut)
98+ if (hOut != null && hOut != INVALID_HANDLE_VALUE ) {
99+ CloseHandle (hOut)
100+ hOut = null
101+ }
99102 CloseHandle (pi.hThread)
100103 CloseHandle (pi.hProcess)
101104 _wunlink (outPath.wideCString(this ))
@@ -109,6 +112,13 @@ internal actual suspend fun executeCommand(
109112 }
110113 val exitCode = exitCodeVar.value.toInt()
111114
115+ // 🔑 Close writer before reading back
116+ if (hOut != null && hOut != INVALID_HANDLE_VALUE ) {
117+ FlushFileBuffers (hOut)
118+ CloseHandle (hOut)
119+ hOut = null
120+ }
121+
112122 // 7) read back bounded
113123 val hIn: HANDLE ? = CreateFileW (
114124 /* lpFileName = */ outPath,
@@ -157,7 +167,10 @@ internal actual suspend fun executeCommand(
157167 CloseHandle (pi.hProcess)
158168 }
159169 } finally {
160- CloseHandle (hOut)
170+ if (hOut != null && hOut != INVALID_HANDLE_VALUE ) {
171+ CloseHandle (hOut)
172+ hOut = null
173+ }
161174 }
162175 }
163176}
0 commit comments