Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPServerWithQuiescingDemo prints an error if the main code is wrapped in a function #219

Open
crontab opened this issue Mar 11, 2024 · 3 comments

Comments

@crontab
Copy link
Contributor

crontab commented Mar 11, 2024

Expected behavior

The demo should be able to shutdown gracefully on SIGINT.

Actual behavior

If the top-level code is wrapped in a function like so:

func run() throws {
    // main top-level code, unchanged
}

try run()

then upon receiving SIGINT the app prints:

ERROR: Cannot schedule tasks on an EventLoop that has already shut down.
    This will be upgraded to a forced crash in future SwiftNIO versions.

and seems to shut down everything and exit normally.

This is a pretty big inconvenience as in any more or less complex project the code that bootstraps the server will most likely be in a function.

The main suspicion is that some destructor is called at the end of run() that tries to use an EventLoop. No idea which object in the main code that might be. Any help would be greatly appreciated!

SwiftNIO-Extras version/commit hash

Current HEAD, a33bb16

Swift & OS version

Swift 5.10
macOS 14.3.1 (23D60)

@crontab
Copy link
Contributor Author

crontab commented Mar 11, 2024

Oh, just found the solution, will post it here.

The function should isolate the quiesce object into a smaller block to ensure that its destructor is executed before the call to group.syncShutdownGracefully() like so:

func run() throws {
    let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
    do {
        let quiesce = ServerQuiescingHelper(group: group)
        // ... rest of the code ...
        try fullyShutdownPromise.futureResult.wait()
    }
    try group.syncShutdownGracefully()
}

try run()

Finally, this doesn't print the error!

@crontab
Copy link
Contributor Author

crontab commented Mar 11, 2024

P.S. may a suggest a modification to the demo app that fixes this?

@Lukasa
Copy link
Contributor

Lukasa commented Mar 15, 2024

Absolutely, a patch would be welcomed that did that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants