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.
This commit is contained in:
Johannes Weiss authored and GitHub committed 2021-02-17 08:33:22 +00:00
1 parent 63d356819c
commit 8b062c0317
1 file changed
+16 -15
+16 -15
View File
@@ -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<Result<T, Error>>()
// 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<Result<Value, Error>>()
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()
}
}