mirror of
https://github.com/apple/swift-nio
synced 2026-11-30 05:28:44 +00:00
### Motivation: SwiftNIO allows and encourages to precisely manage all operating system resources like file descriptors & threads. That is a very important property of the system but in many places -- especially since Swift Concurrency arrived -- many simpler SwiftNIO programs only require a single, globally shared EventLoopGroup. Often even with just one thread. Long story short: Many, probably most users would happily trade precise control over the threads for not having to pass around `EventLoopGroup`s. Today, many of those users resort to creating (and often leaking) threads because it's simpler. Adding a `.globalSingle` static var which _lazily_ provides singleton `EventLoopGroup`s and `NIOThreadPool`s is IMHO a much better answer. Finally, this aligns SwiftNIO a little more with Apple's SDKs which have a lot of global singletons that hold onto system resources (`Dispatch`'s thread pool, `URLSession.shared`, ...). At least in `Dispatch`'s case the Apple SDKs actually make it impossible to manage the resources, there can only ever be one global pool of threads. That's fine for app development but wouldn't be good enough for certain server use cases, therefore I propose to add `NIOSingleton`s as an _option_ to the user. Confident programmers (especially in libraries) are still free and encouraged to manage all the resources deterministically and explicitly. Companion PRs: - https://github.com/apple/swift-nio-transport-services/pull/180 - https://github.com/swift-server/async-http-client/pull/697 ### Modifications: - Add the `NIOSingletons` type - Add `MultiThreadedEventLoopGroup.singleton` - Add `NIOThreadPool.singleton` ### Result: - Easier use of NIO that requires fewer parameters for users who don't require full control. - Helps with #2142 - Fixes #2472 - Partially addresses #2473
182 lines
6.5 KiB
Swift
182 lines
6.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2020-2021 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
#if !canImport(Darwin) || os(macOS)
|
|
import NIOCore
|
|
import NIOPosix
|
|
|
|
fileprivate let group = MultiThreadedEventLoopGroup(numberOfThreads: 2)
|
|
|
|
struct EventLoopCrashTests {
|
|
let testMultiThreadedELGCrashesOnZeroThreads = CrashTest(
|
|
regex: "Precondition failed: numberOfThreads must be positive"
|
|
) {
|
|
try? MultiThreadedEventLoopGroup(numberOfThreads: 0).syncShutdownGracefully()
|
|
}
|
|
|
|
let testWaitCrashesWhenOnEL = CrashTest(
|
|
regex: #"Precondition failed: BUG DETECTED: wait\(\) must not be called when on an EventLoop"#
|
|
) {
|
|
let promise = group.next().makePromise(of: Void.self)
|
|
try? group.next().submit {
|
|
try? promise.futureResult.wait()
|
|
}.wait()
|
|
}
|
|
|
|
let testAssertInEventLoop = CrashTest(
|
|
regex: "Precondition failed"
|
|
) {
|
|
group.next().assertInEventLoop(file: "DUMMY", line: 42)
|
|
}
|
|
|
|
let testPreconditionInEventLoop = CrashTest(
|
|
regex: "Precondition failed"
|
|
) {
|
|
group.next().preconditionInEventLoop(file: "DUMMY", line: 42)
|
|
}
|
|
|
|
let testAssertNotInEventLoop = CrashTest(
|
|
regex: "Precondition failed"
|
|
) {
|
|
let el = group.next()
|
|
try? el.submit {
|
|
el.assertNotInEventLoop(file: "DUMMY", line: 42)
|
|
}.wait()
|
|
}
|
|
|
|
let testPreconditionNotInEventLoop = CrashTest(
|
|
regex: "Precondition failed"
|
|
) {
|
|
let el = group.next()
|
|
try? el.submit {
|
|
el.preconditionNotInEventLoop(file: "DUMMY", line: 42)
|
|
}.wait()
|
|
}
|
|
|
|
let testSchedulingEndlesslyInELShutdown = CrashTest(
|
|
regex: #"Precondition failed: EventLoop SelectableEventLoop \{ .* \} didn't quiesce after 1000 ticks."#
|
|
) {
|
|
let group = MultiThreadedEventLoopGroup.init(numberOfThreads: 1)
|
|
defer {
|
|
try? group.syncShutdownGracefully()
|
|
exit(4)
|
|
}
|
|
let el = group.next()
|
|
el.scheduleTask(in: .hours(7)) {
|
|
// Will never happen.
|
|
exit(1)
|
|
}.futureResult.whenFailure { error in
|
|
guard case .some(.shutdown) = error as? EventLoopError else {
|
|
exit(2)
|
|
}
|
|
func f() {
|
|
el.scheduleTask(in: .nanoseconds(0)) { [f /* to make 5.1 compiler not crash */] in
|
|
f()
|
|
}.futureResult.whenFailure { [f /* to make 5.1 compiler not crash */] error in
|
|
guard case .some(.shutdown) = error as? EventLoopError else {
|
|
exit(3)
|
|
}
|
|
f()
|
|
}
|
|
}
|
|
f()
|
|
}
|
|
}
|
|
|
|
let testLeakingAPromiseCrashes = CrashTest(
|
|
regex: #"Fatal error: leaking promise created at"#
|
|
) {
|
|
@inline(never)
|
|
func leaker() {
|
|
_ = group.next().makePromise(of: Void.self)
|
|
}
|
|
leaker()
|
|
for el in group.makeIterator() {
|
|
try! el.submit {}.wait()
|
|
}
|
|
}
|
|
|
|
let testUsingTheSingletonGroupWhenDisabled = CrashTest(
|
|
regex: #"Fatal error: Cannot create global singleton MultiThreadedEventLoopGroup because the global singletons"#
|
|
) {
|
|
NIOSingletons.singletonsEnabledSuggestion = false
|
|
try? NIOSingletons.posixEventLoopGroup.next().submit {}.wait()
|
|
}
|
|
|
|
let testUsingTheSingletonBlockingPoolWhenDisabled = CrashTest(
|
|
regex: #"Fatal error: Cannot create global singleton NIOThreadPool because the global singletons have been"#
|
|
) {
|
|
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
|
|
defer {
|
|
try? group.syncShutdownGracefully()
|
|
}
|
|
NIOSingletons.singletonsEnabledSuggestion = false
|
|
try? NIOSingletons.posixBlockingThreadPool.runIfActive(eventLoop: group.next(), {}).wait()
|
|
}
|
|
|
|
let testDisablingSingletonsEnabledValueTwice = CrashTest(
|
|
regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
|
|
) {
|
|
NIOSingletons.singletonsEnabledSuggestion = false
|
|
NIOSingletons.singletonsEnabledSuggestion = false
|
|
}
|
|
|
|
let testEnablingSingletonsEnabledValueTwice = CrashTest(
|
|
regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
|
|
) {
|
|
NIOSingletons.singletonsEnabledSuggestion = true
|
|
NIOSingletons.singletonsEnabledSuggestion = true
|
|
}
|
|
|
|
let testEnablingThenDisablingSingletonsEnabledValue = CrashTest(
|
|
regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
|
|
) {
|
|
NIOSingletons.singletonsEnabledSuggestion = true
|
|
NIOSingletons.singletonsEnabledSuggestion = false
|
|
}
|
|
|
|
let testSettingTheSingletonEnabledValueAfterUse = CrashTest(
|
|
regex: #"Fatal error: Bug in user code: Global singleton enabled suggestion has been changed after"#
|
|
) {
|
|
try? MultiThreadedEventLoopGroup.singleton.next().submit({}).wait()
|
|
NIOSingletons.singletonsEnabledSuggestion = true
|
|
}
|
|
|
|
let testSettingTheSuggestedSingletonGroupCountTwice = CrashTest(
|
|
regex: #"Fatal error: Bug in user code: Global singleton suggested loop/thread count has been changed after"#
|
|
) {
|
|
NIOSingletons.groupLoopCountSuggestion = 17
|
|
NIOSingletons.groupLoopCountSuggestion = 17
|
|
}
|
|
|
|
let testSettingTheSuggestedSingletonGroupChangeAfterUse = CrashTest(
|
|
regex: #"Fatal error: Bug in user code: Global singleton suggested loop/thread count has been changed after"#
|
|
) {
|
|
try? MultiThreadedEventLoopGroup.singleton.next().submit({}).wait()
|
|
NIOSingletons.groupLoopCountSuggestion = 17
|
|
}
|
|
|
|
let testSettingTheSuggestedSingletonGroupLoopCountToZero = CrashTest(
|
|
regex: #"Precondition failed: illegal value: needs to be strictly positive"#
|
|
) {
|
|
NIOSingletons.groupLoopCountSuggestion = 0
|
|
}
|
|
|
|
let testSettingTheSuggestedSingletonGroupLoopCountToANegativeValue = CrashTest(
|
|
regex: #"Precondition failed: illegal value: needs to be strictly positive"#
|
|
) {
|
|
NIOSingletons.groupLoopCountSuggestion = -1
|
|
}
|
|
}
|
|
#endif
|