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

Socket mode in a tokio task breaks on Ctrl-C. #231

Closed
Gremious opened this issue Dec 18, 2023 · 6 comments
Closed

Socket mode in a tokio task breaks on Ctrl-C. #231

Gremious opened this issue Dec 18, 2023 · 6 comments
Labels
question Further information is requested

Comments

@Gremious
Copy link

Putting socket_mode_listener.serve().await; into a tokio::spawn(async move { ... }), then hitting Ctrl-C makes tokio panic with

thread 'ctrl-c' panicked at C:\Users\Gremious\.cargo\registry\src\index.crates.io-6f17d22bba15001f\slack-morphism-1.16.1\src\hyper_tokio\socket_mode\tokio_clients_manager.rs:169:48:
Could not send signal on channel.: SendError { .. }

This is on Windows with PowerShell.

Letting the socket sit in actual main and hitting Ctrl-C there works just fine, thankfully.

@abdolence
Copy link
Owner

abdolence commented Dec 18, 2023

Hey,
if you want to use your own lifecycle instead of serve, there is normal API with start().await and shutdown().await, so you can handle signals yourself and have other lifecycle you need.

serve is just a wrapping method created for convenience/examples.

@abdolence abdolence added the question Further information is requested label Dec 18, 2023
@Gremious
Copy link
Author

Gremious commented Dec 18, 2023

Oh I just wanted to run it in main, and hit Ctrl-C to exit, nothing fancy. So just using serve for convenience.

Thing is, I also run a client/listener for a discord bot in the same app, so at least one of those 2 awaits nees to go into their own tokio task.

Sending a Ctrl-C while discord serenity's client is inisde a task is handled gracefuly, thankfully, so I just do that - but Morphism - not so much.

@abdolence
Copy link
Owner

Yeah, as I suspected you have other stuff to run and await :)
You need to use

  1. slack.start().await <- this will be returned as soon as it is ready
  2. discord_client.start().await // Not sure if they're blocking thread or not
  3. your own handler for signals (you can use ctrlc yourself if you want)
  4. slack.shutdown().await <- to disconnect from Slack server for Socket Mode
  5. other cleanups (discord?)

@abdolence
Copy link
Owner

abdolence commented Dec 18, 2023

The nature of an error is coming from the ctrlc implementation:
https://detegr.github.io/doc/ctrlc/fn.set_handler.html#

Should only be called once, typically at the start of your program

I suspect someone else already set it. I don't have a windows machine to debug it more, so if you are able to find a better implementation for windows, let me know :)

@Gremious
Copy link
Author

Gremious commented Dec 18, 2023

Ahh, thank you, your example actually makes the api a lot clearer. I (wasn't awake enough and) didn't read hard enough into it, so I had assumed I would've needed to setup events to send signals or some such, but it's just two split calls - appreciate it!

My qualms have been resolved (Thanks!), so I'll let you handle closing the issue in the hopes that a good ctrl-c lib that fixes this comes along ?

@Gremious Gremious reopened this Dec 18, 2023
@abdolence
Copy link
Owner

Well, if it works for you, then I'll close this issue, since as I mentioned, serve is just using ctrlc for Windows (if you look at the source code, it just 3-4 lines of code).
In real life application start/shutdown is a way to go.

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

No branches or pull requests

2 participants