Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions Sources/SwiftBuild/SWBBuildServiceConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ typealias swb_build_service_connection_message_handler_t = @Sendable (UInt64, SW

/// Track whether the channels have been cleared because the service has crashed.
var channelsHaveBeenCleared = false

/// A nil handler for a known channel is expected, service crash or client closed the channel before the reply arrived.
/// A nil handler for an unknown channel is unexpected.
func isChannelKnown(_ channel: UInt64) -> Bool {
return channel <= nextChannelID
}
}

/// An "unfair lock" that protects the channels state. Should be held only for very short periods of time.
Expand Down Expand Up @@ -303,14 +309,6 @@ typealias swb_build_service_connection_message_handler_t = @Sendable (UInt64, SW
let channel = headerFrame.channel
let msgSize = Int(headerFrame.messageSize)

// Look up the channel by its number.
let handler = self.channelState.withLock({ $0.channels[channel] })
if handler == nil {
// It’s an error if we didn’t find a channel here.
// FIXME: We need to handle this error in a better way.
assertionFailure("no handler for channel: \(channel)")
}

// If we don’t have all the data yet, we can go no further.
if offset + msgSize > data.count {
// Move the offset back to just before the header, so we’ll see it again next time around.
Expand All @@ -320,7 +318,15 @@ typealias swb_build_service_connection_message_handler_t = @Sendable (UInt64, SW
}

// Otherwise, look up the channel by its number.
handler?(channel, data.subdata(in: offset..<(offset + msgSize)))
let (handler, isChannelKnown) = self.channelState.withLock {
return ($0.channels[channel], $0.isChannelKnown(channel))
}

if let handler {
handler(channel, data.subdata(in: offset..<(offset + msgSize)))
} else {
assert(isChannelKnown, "Received reply for unknown channel: \(channel)")
}

// Move on to the next message.
offset += msgSize
Expand Down
Loading