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

fix: gracefully exit when misconfigured or unavailable audio backend #1390

Merged
merged 1 commit into from
Feb 6, 2024

Conversation

ThomasFrans
Copy link
Contributor

@ThomasFrans ThomasFrans commented Feb 4, 2024

When the user has an error in their audio backend configuration or doesn't have audio backends available, gracefully exit instead of panicking.

Describe your changes

Move audio backend initialization into the main thread for simpler error handling. Propagate errors up to the main function and log/print them there before quitting. This makes sure that everything that needs to be dropped is dropped automatically (e.g. not the case when manual exit from Application::new() which needs manual drop of CursiveRunner to reset terminal).

Issue ticket number and link

closes #1383

Checklist before requesting a review

  • Documentation was updated (i.e. due to changes in keybindings, commands, etc.)
  • Changelog was updated with relevant user-facing changes (eg. not dependency updates,
    not performance improvements, etc.)

@ThomasFrans
Copy link
Contributor Author

I tried to limit the changes at first by leaving backend creation up to the worker thread (the way it currently is in the main branch). When an error occurs, that thread either has to return which causes lots of panics elsewhere due to disconnected channels, or needs to sleep until ncspot has quit which doesn't work as the worker thread is needed to get to the event loop (token update, getting user...). After lots of trial (and even more error), I think the approach in this PR is the simplest.

Comment on lines -94 to -109
{
let worker_channel = self.channel.clone();
let cfg = self.cfg.clone();
let events = self.events.clone();
let volume = self.volume();
let credentials = self.credentials.clone();
ASYNC_RUNTIME.get().unwrap().spawn(Self::worker(
worker_channel,
events,
rx,
cfg,
credentials,
user_tx,
volume,
));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know why this scope was here. I don't think it does anything.

src/application.rs Outdated Show resolved Hide resolved
src/application.rs Show resolved Hide resolved
src/main.rs Outdated Show resolved Hide resolved
When the user has an error in their audio backend configuration or
doesn't have audio backends available, gracefully exit instead of
panicking.
@ThomasFrans ThomasFrans force-pushed the fix-unknown-backend-panic branch from 7f4f74a to 7eaf748 Compare February 5, 2024 11:17
@ThomasFrans ThomasFrans marked this pull request as ready for review February 5, 2024 11:19
Copy link
Owner

@hrkfdn hrkfdn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +68 to +69
eprintln!("{error}");
error!("{error}");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it could be nice to have the complete picture of what happened in the log, as well as a message to stderr to show the user that this was expected (not a crash) and why it quit.

Copy link
Owner

@hrkfdn hrkfdn Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, does log::error log to stdout or stderr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's set up to log to either. It only logs to the file if I remember correctly, because stdout/stderr wouldn't work during most of the program.

Copy link
Owner

@hrkfdn hrkfdn Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, right, this is still a curses application 🤦

@hrkfdn hrkfdn merged commit 38010b4 into hrkfdn:main Feb 6, 2024
5 checks passed
@ThomasFrans ThomasFrans deleted the fix-unknown-backend-panic branch February 6, 2024 19:53
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

Successfully merging this pull request may close these issues.

Panic when setting unknown audio backend
2 participants