From 8b062c0317e8e12e466cd4f541f54f7cd68e89f9 Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Wed, 17 Feb 2021 08:33:22 +0000 Subject: [PATCH] SAL: more sync between test and EL threads (#1749) Motivation: The SAL (Syscall Abstraction Layer) virtualises pretty much all system resources that NIO uses, except time and threads. That means that unless explicitly made sure, the EL thread and the test thread run at their own pace. The synchronisation point is whenever the EL thread makes a (virtualised) syscall which the test thread then sees (and asserts). The problem in #1748 was that we were assuming that the EL thread would park itself very fast. So when the test driver thread came along to execute something on the EL, it just assumed that the EL has already parked (ie. waited for a wakeup). Seemingly that works most of the time but not always. Modifications: - Always actually wait for the EL to park itself. - Delete duplicated code in `salWait` to use `runSAL` - fixes #1748 Result: Fewer races. --- Tests/NIOTests/SyscallAbstractionLayer.swift | 31 ++++++++++---------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Tests/NIOTests/SyscallAbstractionLayer.swift b/Tests/NIOTests/SyscallAbstractionLayer.swift index 50c00b275..63b528d26 100644 --- a/Tests/NIOTests/SyscallAbstractionLayer.swift +++ b/Tests/NIOTests/SyscallAbstractionLayer.swift @@ -118,7 +118,7 @@ extension LockedBox where T == UserToKernel { func assertParkedRightNow(file: StaticString = #file, line: UInt = #line) throws { SAL.printIfDebug("\(#function)") let syscall = try self.waitForValue() - if case .whenReady = syscall { + if case .whenReady(.block) = syscall { return } else { XCTFail("unexpected syscall \(syscall)", file: (file), line: line) @@ -421,6 +421,12 @@ extension EventLoop { _ body: @escaping () throws -> T) throws -> T { let hookedSelector = ((self as! SelectableEventLoop)._selector as! HookedSelector) let box = LockedBox>() + + // To prevent races between the test driver thread (this thread) and the EventLoop (another thread), we need + // to wait for the EventLoop to finish its tick and park itself. That makes sure both threads are synchronised + // so we know exactly what the EventLoop thread is currently up to (nothing at all, waiting for a wakeup). + try hookedSelector.userToKernel.assertParkedRightNow() + self.execute { do { try box.waitForEmptyAndSet(.init(catching: body)) @@ -430,7 +436,9 @@ extension EventLoop { } try hookedSelector.assertWakeup(file: (file), line: line) try syscallAssertions() - try hookedSelector.userToKernel.assertParkedRightNow() // We need the EventLoop to finish running its tick. + + // Here as well, we need to synchronise and wait for the EventLoop to finish running its tick. + try hookedSelector.userToKernel.assertParkedRightNow() return try box.takeValue().get() } } @@ -441,22 +449,15 @@ extension EventLoopFuture { /// Using a plain `EventLoopFuture.wait()` together with the SAL would require you to spin the `EventLoop` manually /// which is error prone and hard. internal func salWait() throws -> Value { - assert(Thread.isMainThread) let box = LockedBox>() - let hookedSelector = ((self.eventLoop as! SelectableEventLoop)._selector as! HookedSelector) - self.eventLoop.execute { - self.whenComplete { result in - do { - try box.waitForEmptyAndSet(result) - } catch { - box.value = .failure(error) - } + XCTAssertNoThrow(try self.eventLoop.runSAL() { + self.whenComplete { value in + // We can bang this because the LockedBox is empty so it'll immediately succeed. + try! box.waitForEmptyAndSet(value) } - } - try hookedSelector.assertWakeup() - try hookedSelector.userToKernel.assertParkedRightNow() // We need the EventLoop to finish running its tick. - return try box.takeValue().get() + }) + return try box.waitForValue().get() } }