Skip to content

Commit b1b9980

Browse files
Fix: Kubes Cluster in Container Crashing Container (IS#923) (#930)
- Fixes issue #923 - I fixed a race condition in `ConnectHandler.swift` where an asynchronous network connection could complete after the handler had already been removed from the pipeline. - This prevents the EXC_BREAKPOINT crash in container-runtime-linux that occurred when kinc (Kubernetes in Container) created rapid connections. - The actual fix was inadvertently applied in #957, so this PR contains only the test code.
1 parent 9f4efe0 commit b1b9980

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2025 Apple Inc. and the container project authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
import NIO
18+
import Testing
19+
20+
@testable import SocketForwarder
21+
22+
struct ConnectHandlerRaceTest {
23+
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
24+
25+
@Test
26+
func testRapidConnectDisconnect() async throws {
27+
let requestCount = 500
28+
29+
let serverAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
30+
let server = TCPEchoServer(serverAddress: serverAddress, eventLoopGroup: eventLoopGroup)
31+
let serverChannel = try await server.run().get()
32+
let actualServerAddress = try #require(serverChannel.localAddress)
33+
34+
let proxyAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 0)
35+
let forwarder = try TCPForwarder(
36+
proxyAddress: proxyAddress,
37+
serverAddress: actualServerAddress,
38+
eventLoopGroup: eventLoopGroup
39+
)
40+
let forwarderResult = try await forwarder.run().get()
41+
let actualProxyAddress = try #require(forwarderResult.proxyAddress)
42+
43+
try await withThrowingTaskGroup(of: Void.self) { group in
44+
for _ in 0..<requestCount {
45+
group.addTask {
46+
do {
47+
let channel = try await ClientBootstrap(group: self.eventLoopGroup)
48+
.connect(to: actualProxyAddress)
49+
.get()
50+
51+
try await channel.close()
52+
} catch {
53+
// Going to ignore connection errors as we are intentionally stressing it
54+
}
55+
}
56+
}
57+
try await group.waitForAll()
58+
}
59+
60+
serverChannel.eventLoop.execute { _ = serverChannel.close() }
61+
try await serverChannel.closeFuture.get()
62+
63+
forwarderResult.close()
64+
try await forwarderResult.wait()
65+
}
66+
}

0 commit comments

Comments
 (0)