diff --git a/Sources/NIOCrashTester/CrashTestSuites.swift b/Sources/NIOCrashTester/CrashTestSuites.swift index 03ec6e76e..a11b93e23 100644 --- a/Sources/NIOCrashTester/CrashTestSuites.swift +++ b/Sources/NIOCrashTester/CrashTestSuites.swift @@ -17,4 +17,5 @@ let crashTestSuites: [String: Any] = [ "ByteBufferCrashTests": ByteBufferCrashTests(), "SystemCrashTests": SystemCrashTests(), "HTTPCrashTests": HTTPCrashTests(), + "StrictCrashTests": StrictCrashTests(), ] diff --git a/Sources/NIOCrashTester/CrashTests+Strict.swift b/Sources/NIOCrashTester/CrashTests+Strict.swift new file mode 100644 index 000000000..533cdec73 --- /dev/null +++ b/Sources/NIOCrashTester/CrashTests+Strict.swift @@ -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.") + } + } +} diff --git a/Sources/NIOPosix/SelectableEventLoop.swift b/Sources/NIOPosix/SelectableEventLoop.swift index 29f6c2c1f..35e4372e3 100644 --- a/Sources/NIOPosix/SelectableEventLoop.swift +++ b/Sources/NIOPosix/SelectableEventLoop.swift @@ -34,6 +34,16 @@ internal func withAutoReleasePool(_ 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 }