-
Notifications
You must be signed in to change notification settings - Fork 28
Unify tests on all supported platforms to ensure consistent behavior and add more tests. #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iCharlesHu
wants to merge
1
commit into
swiftlang:main
Choose a base branch
from
iCharlesHu:charles/testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,401
−1,726
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,8 +67,9 @@ final class AsyncIO: Sendable { | |
static let shared: AsyncIO = AsyncIO() | ||
|
||
private let state: Result<State, SubprocessError> | ||
private let shutdownFlag: Atomic<UInt8> = Atomic(0) | ||
|
||
private init() { | ||
internal init() { | ||
// Create main epoll fd | ||
let epollFileDescriptor = epoll_create1(CInt(EPOLL_CLOEXEC)) | ||
guard epollFileDescriptor >= 0 else { | ||
|
@@ -204,11 +205,15 @@ final class AsyncIO: Sendable { | |
} | ||
} | ||
|
||
private func shutdown() { | ||
internal func shutdown() { | ||
guard case .success(let currentState) = self.state else { | ||
return | ||
} | ||
|
||
guard self.shutdownFlag.add(1, ordering: .sequentiallyConsistent).newValue == 1 else { | ||
// We already closed this AsyncIO | ||
return | ||
} | ||
var one: UInt64 = 1 | ||
// Wake up the thread for shutdown | ||
_ = _SubprocessCShims.write(currentState.shutdownFileDescriptor, &one, MemoryLayout<UInt64>.stride) | ||
|
@@ -226,7 +231,6 @@ final class AsyncIO: Sendable { | |
} | ||
} | ||
|
||
|
||
private func registerFileDescriptor( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should registerFileDescriptor precondition that the fd is not already in the |
||
_ fileDescriptor: FileDescriptor, | ||
for event: Event | ||
|
@@ -277,11 +281,12 @@ final class AsyncIO: Sendable { | |
&event | ||
) | ||
if rc != 0 { | ||
let capturedError = errno | ||
let error = SubprocessError( | ||
code: .init(.asyncIOFailed( | ||
"failed to add \(fileDescriptor.rawValue) to epoll list") | ||
), | ||
underlyingError: .init(rawValue: errno) | ||
underlyingError: .init(rawValue: capturedError) | ||
) | ||
continuation.finish(throwing: error) | ||
return | ||
|
@@ -344,6 +349,9 @@ extension AsyncIO { | |
from fileDescriptor: FileDescriptor, | ||
upTo maxLength: Int | ||
) async throws -> [UInt8]? { | ||
guard maxLength > 0 else { | ||
return nil | ||
} | ||
// If we are reading until EOF, start with readBufferSize | ||
// and gradually increase buffer size | ||
let bufferLength = maxLength == .max ? readBufferSize : maxLength | ||
|
@@ -407,6 +415,7 @@ extension AsyncIO { | |
} | ||
} | ||
} | ||
resultBuffer.removeLast(resultBuffer.count - readLength) | ||
return resultBuffer | ||
} | ||
|
||
|
@@ -421,6 +430,9 @@ extension AsyncIO { | |
_ bytes: Bytes, | ||
to diskIO: borrowing IOChannel | ||
) async throws -> Int { | ||
guard bytes.count > 0 else { | ||
return 0 | ||
} | ||
let fileDescriptor = diskIO.channel | ||
let signalStream = self.registerFileDescriptor(fileDescriptor, for: .write) | ||
var writtenLength: Int = 0 | ||
|
@@ -464,6 +476,9 @@ extension AsyncIO { | |
_ span: borrowing RawSpan, | ||
to diskIO: borrowing IOChannel | ||
) async throws -> Int { | ||
guard span.byteCount > 0 else { | ||
return 0 | ||
} | ||
let fileDescriptor = diskIO.channel | ||
let signalStream = self.registerFileDescriptor(fileDescriptor, for: .write) | ||
var writtenLength: Int = 0 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,10 +51,10 @@ final class AsyncIO: @unchecked Sendable { | |
static let shared = AsyncIO() | ||
|
||
private let ioCompletionPort: Result<HANDLE, SubprocessError> | ||
|
||
private let monitorThread: Result<HANDLE, SubprocessError> | ||
private let shutdownFlag: Atomic<UInt8> = Atomic(0) | ||
|
||
private init() { | ||
internal init() { | ||
var maybeSetupError: SubprocessError? = nil | ||
// Create the the completion port | ||
guard let port = CreateIoCompletionPort( | ||
|
@@ -78,10 +78,11 @@ final class AsyncIO: @unchecked Sendable { | |
/// > thread management rather than CreateThread and ExitThread | ||
let threadHandleValue = _beginthreadex(nil, 0, { args in | ||
func reportError(_ error: SubprocessError) { | ||
_registration.withLock { store in | ||
for continuation in store.values { | ||
continuation.finish(throwing: error) | ||
} | ||
let continuations = _registration.withLock { store in | ||
return store.values | ||
} | ||
for continuation in continuations { | ||
continuation.finish(throwing: error) | ||
} | ||
} | ||
|
||
|
@@ -110,11 +111,13 @@ final class AsyncIO: @unchecked Sendable { | |
// in the store. Windows does not offer an API to remove a | ||
// HANDLE from an IOCP port, therefore we leave the registration | ||
// to signify the HANDLE has already been resisted. | ||
_registration.withLock { store in | ||
let continuation = _registration.withLock { store -> SignalStream.Continuation? in | ||
if let continuation = store[targetFileDescriptor] { | ||
continuation.finish() | ||
return continuation | ||
} | ||
return nil | ||
} | ||
continuation?.finish() | ||
continue | ||
} else { | ||
let error = SubprocessError( | ||
|
@@ -159,12 +162,17 @@ final class AsyncIO: @unchecked Sendable { | |
} | ||
} | ||
|
||
private func shutdown() { | ||
// Post status to shutdown HANDLE | ||
internal func shutdown() { | ||
guard case .success(let ioPort) = ioCompletionPort, | ||
case .success(let monitorThreadHandle) = monitorThread else { | ||
return | ||
} | ||
// Make sure we don't shutdown the same instance twice | ||
guard self.shutdownFlag.add(1, ordering: .relaxed).newValue == 1 else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to be >=1? |
||
// We already closed this AsyncIO | ||
return | ||
} | ||
// Post status to shutdown HANDLE | ||
PostQueuedCompletionStatus( | ||
ioPort, // CompletionPort | ||
0, // Number of bytes transferred. | ||
|
@@ -245,6 +253,9 @@ final class AsyncIO: @unchecked Sendable { | |
from handle: HANDLE, | ||
upTo maxLength: Int | ||
) async throws -> [UInt8]? { | ||
guard maxLength > 0 else { | ||
return nil | ||
} | ||
// If we are reading until EOF, start with readBufferSize | ||
// and gradually increase buffer size | ||
let bufferLength = maxLength == .max ? readBufferSize : maxLength | ||
|
@@ -284,8 +295,12 @@ final class AsyncIO: @unchecked Sendable { | |
// Make sure we only get `ERROR_IO_PENDING` or `ERROR_BROKEN_PIPE` | ||
let lastError = GetLastError() | ||
if lastError == ERROR_BROKEN_PIPE { | ||
// We reached EOF | ||
return nil | ||
// We reached EOF. Return whatever's left | ||
guard readLength > 0 else { | ||
return nil | ||
} | ||
resultBuffer.removeLast(resultBuffer.count - readLength) | ||
return resultBuffer | ||
} | ||
guard lastError == ERROR_IO_PENDING else { | ||
let error = SubprocessError( | ||
|
@@ -337,6 +352,9 @@ final class AsyncIO: @unchecked Sendable { | |
_ span: borrowing RawSpan, | ||
to diskIO: borrowing IOChannel | ||
) async throws -> Int { | ||
guard span.byteCount > 0 else { | ||
return 0 | ||
} | ||
let handle = diskIO.channel | ||
var signalStream = self.registerHandle(diskIO.channel).makeAsyncIterator() | ||
var writtenLength: Int = 0 | ||
|
@@ -389,6 +407,9 @@ final class AsyncIO: @unchecked Sendable { | |
_ bytes: Bytes, | ||
to diskIO: borrowing IOChannel | ||
) async throws -> Int { | ||
guard bytes.count > 0 else { | ||
return 0 | ||
} | ||
let handle = diskIO.channel | ||
var signalStream = self.registerHandle(diskIO.channel).makeAsyncIterator() | ||
var writtenLength: Int = 0 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be >=1 instead of ==1? I think as written this will run the shutdown procedure n-1 times for n calls.