Repository navigation
Harden socket response writes #3355
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
Merged
Merged
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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import XCTest | ||
| import Darwin | ||
| import Foundation | ||
|
|
||
| #if canImport(cmux_DEV) | ||
| @testable import cmux_DEV | ||
| #elseif canImport(cmux) | ||
| @testable import cmux | ||
| #endif | ||
|
|
||
| @MainActor | ||
| final class TerminalControllerSocketWriteTests: XCTestCase { | ||
| func testSocketWriteAllWritesCompletePayload() throws { | ||
| let sockets = try makeSocketPair() | ||
| defer { | ||
| Darwin.close(sockets.reader) | ||
| Darwin.close(sockets.writer) | ||
| } | ||
|
|
||
| let payload = Data("PONG\n".utf8) | ||
| XCTAssertTrue(TerminalController.writeAllToSocket(payload, to: sockets.writer)) | ||
|
|
||
| var buffer = [UInt8](repeating: 0, count: payload.count) | ||
| let count = Darwin.read(sockets.reader, &buffer, buffer.count) | ||
| XCTAssertEqual(count, payload.count) | ||
| XCTAssertEqual(count > 0 ? Data(buffer.prefix(count)) : Data(), payload) | ||
| } | ||
|
|
||
| func testSocketWriteAllReturnsWhenPeerDoesNotRead() throws { | ||
| let sockets = try makeSocketPair() | ||
| defer { | ||
| Darwin.close(sockets.reader) | ||
| Darwin.close(sockets.writer) | ||
| } | ||
| try configureSendTimeout(sockets.writer, timeout: 0.05) | ||
|
|
||
| let payload = Data(repeating: 0x78, count: 8 * 1024 * 1024) | ||
| let startedAt = Date() | ||
| XCTAssertFalse(TerminalController.writeAllToSocket(payload, to: sockets.writer)) | ||
| XCTAssertLessThan(Date().timeIntervalSince(startedAt), 2.0) | ||
| } | ||
|
|
||
| private nonisolated func makeSocketPair() throws -> (reader: Int32, writer: Int32) { | ||
| var fds = [Int32](repeating: -1, count: 2) | ||
| let result = fds.withUnsafeMutableBufferPointer { buffer in | ||
| Darwin.socketpair(AF_UNIX, SOCK_STREAM, 0, buffer.baseAddress) | ||
| } | ||
| guard result == 0 else { | ||
| throw posixError("socketpair(AF_UNIX)") | ||
| } | ||
| return (reader: fds[0], writer: fds[1]) | ||
| } | ||
|
|
||
| private nonisolated func configureSendTimeout(_ fd: Int32, timeout: TimeInterval) throws { | ||
| let seconds = floor(max(timeout, 0)) | ||
| let microseconds = (max(timeout, 0) - seconds) * 1_000_000 | ||
| var socketTimeout = timeval(tv_sec: Int(seconds), tv_usec: Int32(microseconds.rounded())) | ||
| let result = withUnsafePointer(to: &socketTimeout) { ptr in | ||
| Darwin.setsockopt( | ||
| fd, | ||
| SOL_SOCKET, | ||
| SO_SNDTIMEO, | ||
| ptr, | ||
| socklen_t(MemoryLayout<timeval>.size) | ||
| ) | ||
| } | ||
| guard result == 0 else { | ||
| throw posixError("setsockopt(SO_SNDTIMEO)") | ||
| } | ||
| } | ||
|
|
||
| private nonisolated func posixError(_ operation: String) -> NSError { | ||
| NSError( | ||
| domain: NSPOSIXErrorDomain, | ||
| code: Int(errno), | ||
| userInfo: [NSLocalizedDescriptionKey: "\(operation) failed: \(String(cString: strerror(errno)))"] | ||
| ) | ||
| } | ||
| } |
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.
configureAcceptedClientSocketnow appliesSO_SNDTIMEOto guard against slow-reading clients, but it doesn't applySO_RCVTIMEO. Theread()call inhandleClient(line 1882) will block indefinitely if a client connects and then goes silent without closing the socket — stalling that handler thread for the connection's lifetime. The code comment at line 1864 acknowledges connect-only probes, but a client that sends a partial command and then stalls would hold the thread forever. Consider addingSO_RCVTIMEOalongsideSO_SNDTIMEOto fully bound the accepted-socket lifecycle.