Repository: apple/swift-nio
File: Sources/NIOPosix/NIOThreadPool.swift (Swift)

Code before:
        guard self.canBeStopped else {
            queue.async {
                callback(NIOThreadPoolError.UnsupportedOperation())
            }
            return
        }

        let threadsToJoin = self.conditionLock.withLock {
            switch self.state {
            case .running(let items):
                self.state = .modifying
                queue.async {
                    for item in items {
                        item.workItem(.cancelled)
                    }
                }
                self.state = .shuttingDown(Array(repeating: true, count: self.numberOfThreads))

                let threads = self.threads!
                self.threads = nil

                // Each thread has work to do: shutdown.
                return (unlockWith: .hasWork, result: threads)

            case .shuttingDown, .stopped:
                return (unlockWith: .hasWork, result: [])

            case .modifying:
                fatalError(".modifying state misuse")
            }
        }

        DispatchQueue(label: "io.swiftnio.NIOThreadPool.shutdownGracefully").async {
            for thread in threadsToJoin {
                thread.join()
            }
            queue.async {
                callback(nil)
            }
        }
    }

    /// Submit a `WorkItem` to process.
    ///
    /// - Note: This is a low-level method, in most cases the `runIfActive` method should be used.
    ///
    /// - Parameters:
    ///   - body: The `WorkItem` to process by the `NIOThreadPool`.
    @preconcurrency
    public func submit(_ body: @escaping WorkItem) {
        self._submit(id: nil, body)

Review comment (suggestion):
```suggestion
                return (unlockWith: nil, result: [])
```

Code after:
    ///   - callback: The function to be executed once the shutdown is complete.
    @preconcurrency
    public func shutdownGracefully(queue: DispatchQueue, _ callback: @escaping @Sendable (Error?) -> Void) {
        self._shutdownGracefully(queue: queue, callback)
    }

    private func _shutdownGracefully(queue: DispatchQueue, _ callback: @escaping (Error?) -> Void) {
        guard self.canBeStopped else {
            queue.async {
                callback(NIOThreadPoolError.UnsupportedOperation())
            }
            return
        }

        let threadsToJoin = self.conditionLock.withLock {
            switch self.state {
            case .running(let items):
                self.state = .modifying
                queue.async {
                    for item in items {
                        item.workItem(.cancelled)
                    }
                }
                self.state = .shuttingDown(Array(repeating: true, count: self.numberOfThreads))

                let threads = self.threads!
                self.threads = nil

                // Each thread has work to do: shutdown.
                return (unlockWith: .hasWork, result: threads)

            case .shuttingDown, .stopped:
                return (unlockWith: nil, result: [])

            case .modifying:
                fatalError(".modifying state misuse")
            }
        }

        DispatchQueue(label: "io.swiftnio.NIOThreadPool.shutdownGracefully").async {
            for thread in threadsToJoin {
                thread.join()
            }
            queue.async {
                callback(nil)
            }
        }
    }

    /// Submit a `WorkItem` to process.
    ///