Skip to content

Commit eebc39b

Browse files
committed
safely close hOut
1 parent 712f5eb commit eebc39b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

aws-runtime/aws-config/mingw/src/aws/sdk/kotlin/runtime/auth/credentials/executeCommandMingw.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)