mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
Throw fatalError when scheduling on shutdown EL if SWIFTNIO_STRICT is set (#2190)
* Throw fatalError when scheduling on shutdown EL if SWIFTNIO_STRICT is set Signed-off-by: Si Beaumont <beaumont@apple.com> * Add CrashTest for SWIFTNIO_STRICT crash Signed-off-by: Si Beaumont <beaumont@apple.com> * fixup: Extract env var parsing to static let Signed-off-by: Si Beaumont <beaumont@apple.com> Co-authored-by: Cory Benfield <lukasa@apple.com>
This commit is contained in:
1 parent
b393d39603
commit
69bb75df32
3 files changed
+50
-2
No files matched your search
@@ -17,4 +17,5 @@ let crashTestSuites: [String: Any] = [
|
||||
"ByteBufferCrashTests": ByteBufferCrashTests(),
|
||||
"SystemCrashTests": SystemCrashTests(),
|
||||
"HTTPCrashTests": HTTPCrashTests(),
|
||||
"StrictCrashTests": StrictCrashTests(),
|
||||
]
|
||||
@@ -0,0 +1,29 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftNIO open source project
|
||||
//
|
||||
// Copyright (c) 2022 Apple Inc. and the SwiftNIO project authors
|
||||
// Licensed under Apache License v2.0
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import NIOPosix
|
||||
|
||||
struct StrictCrashTests {
|
||||
let testEventLoopSheduleAfterShutdown = CrashTest(
|
||||
regex: "Fatal error: Cannot schedule tasks on an EventLoop that has already shut down."
|
||||
) {
|
||||
setenv("SWIFTNIO_STRICT", "1", 1)
|
||||
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
||||
let loop = elg.next()
|
||||
try! elg.syncShutdownGracefully()
|
||||
loop.execute {
|
||||
print("Crash should happen before this line is printed.")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,16 @@ internal func withAutoReleasePool<T>(_ execute: () throws -> T) rethrows -> T {
|
||||
/// is guaranteed to never change!
|
||||
@usableFromInline
|
||||
internal final class SelectableEventLoop: EventLoop {
|
||||
|
||||
static let strictModeEnabled: Bool = {
|
||||
switch getenv("SWIFTNIO_STRICT").map({ String.init(cString: $0).lowercased() }) {
|
||||
case "true", "y", "yes", "on", "1":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}()
|
||||
|
||||
/// The different state in the lifecycle of an `EventLoop` seen from _outside_ the `EventLoop`.
|
||||
private enum ExternalState {
|
||||
/// `EventLoop` is open and so can process more work.
|
||||
@@ -325,8 +335,16 @@ Further information:
|
||||
} else {
|
||||
let shouldWakeSelector: Bool = self.externalStateLock.withLock {
|
||||
guard self.validExternalStateToScheduleTasks else {
|
||||
print("ERROR: Cannot schedule tasks on an EventLoop that has already shut down. " +
|
||||
"This will be upgraded to a forced crash in future SwiftNIO versions.")
|
||||
if Self.strictModeEnabled {
|
||||
fatalError("Cannot schedule tasks on an EventLoop that has already shut down.")
|
||||
}
|
||||
fputs(
|
||||
"""
|
||||
ERROR: Cannot schedule tasks on an EventLoop that has already shut down. \
|
||||
This will be upgraded to a forced crash in future SwiftNIO versions.\n
|
||||
""",
|
||||
stderr
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user