Skip to content

Commit a2aa54f

Browse files
committed
define RelayState
1 parent c48ac25 commit a2aa54f

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

vminitd/Sources/vminitd/StandardIO.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ final class StandardIO: ManagedProcess.IO & Sendable {
119119
}
120120
}
121121

122+
final class RelayState: @unchecked Sendable {
123+
var done = false
124+
}
125+
122126
// NOP
123127
func resize(size: Terminal.Size) throws {}
124128

@@ -128,10 +132,10 @@ final class StandardIO: ManagedProcess.IO & Sendable {
128132
// `buf` isn't used concurrently.
129133
nonisolated(unsafe) let buf = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: Int(getpagesize()))
130134

131-
var didCleanup = false
132-
let cleanupRelay: @Sendable () -> Void = {
133-
if didCleanup { return }
134-
didCleanup = true
135+
let state = RelayState()
136+
let cleanupRelay: @Sendable () -> Void = { [state] in
137+
guard !state.done else { return }
138+
state.done = true
135139
self.cleanupRelay(readFd: readFromFd, writeFd: writeToFd, buffer: buf, log: self.log)
136140
}
137141

vminitd/Sources/vminitd/TerminalIO.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,20 @@ final class TerminalIO: ManagedProcess.IO & Sendable {
8888
}
8989
}
9090

91+
final class RelayState: @unchecked Sendable {
92+
var done = false
93+
}
94+
9195
func relay(readFromFd: Int32, writeToFd: Int32) throws {
9296
let readFrom = OSFile(fd: readFromFd)
9397
let writeTo = OSFile(fd: writeToFd)
9498
// `buf` isn't used concurrently.
9599
nonisolated(unsafe) let buf = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: Int(getpagesize()))
96100

97-
var didCleanup = false
98-
let cleanupRelay: @Sendable () -> Void = {
99-
if didCleanup { return }
100-
didCleanup = true
101+
let state = RelayState()
102+
let cleanupRelay: @Sendable () -> Void = { [state] in
103+
guard !state.done else { return }
104+
state.done = true
101105
self.cleanupRelay(readFd: readFromFd, writeFd: writeToFd, buffer: buf, log: self.log)
102106
}
103107

0 commit comments

Comments
 (0)