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

Gracefully exit #46

Closed
jian-lin opened this issue Jul 19, 2022 · 6 comments
Closed

Gracefully exit #46

jian-lin opened this issue Jul 19, 2022 · 6 comments
Labels
linux Issue pertains to Linux only

Comments

@jian-lin
Copy link
Contributor

I am trying to add a functionality of creating a symlink in /dev/input/by-id/ for the newly created device /dev/input/eventxx.

Creating a symlink works if #45 is applied. However, kanata doesn't gracefully exit, which makes cleaning up the symlink rather hard if not impossible.

I would like to discuss about graceful exit.

@jtroo jtroo added bug Something isn't working linux Issue pertains to Linux only labels Jul 19, 2022
@jtroo
Copy link
Owner

jtroo commented Jul 19, 2022

Sure, could you say more about what about kanata's exit is not graceful? Would it need to catch an unwind and do some evdev cleanup before fully exiting?

@jtroo jtroo added this to the v1.0.6 milestone Jul 19, 2022
@jian-lin
Copy link
Contributor Author

Here is my WIP commit: jian-lin/kanata@c9427c9

The problem is symlink is not deleted after I Ctrl-C kanata.

Steps to reproduce:

  1. sudo ./target/debug/kanata --cfg cfg_samples/custom.kbd --symlink-name kanata
  2. type Ctrl-C after it starts
  3. /dev/input/by-id/kanata is a broken symlink, which ideally should be deleted

@jtroo
Copy link
Owner

jtroo commented Jul 19, 2022

Right, the issue you've run into is that Rust does not guarantee that Drop implementations will be called for your code, which isn't specific to kanata. You'll need to add custom signal handlers for cleanup.

https://lib.rs/crates/ctrlc might be of help

@jian-lin
Copy link
Contributor Author

Would it need to catch an unwind and do some evdev cleanup before fully exiting

/dev/input/eventxx device is deleted when kanata exits. Not sure why though.

You'll need to add custom signal handlers for cleanup. https://lib.rs/crates/ctrlc might be of help

Yeah, handling signals is a solution.

However I prefer signal-hook because:

  1. the symlink feature is linux-specific so ctrls's cross-platform ability is not needed
  2. systemd by default use SIGTERM and SIGKILL to terminate a process which ctrlc cannot handle

@jtroo
Copy link
Owner

jtroo commented Jul 19, 2022

/dev/input/eventxx device is deleted when kanata exits. Not sure why though.

I would guess this is properly cleaned up and closed because the kernel knows about the kanata process and the fact that it has files related to the evdev interface open. So it's the kernel cleaning up as opposed to Rust code. This is in contrast to the symlink which is a filesystem concept independent of the input devices, so the kernel doesn't care to clean it up, and if the Rust code Drop handlers don't get called, it won't be cleaned up.

systemd by default use SIGTERM and SIGKILL to terminate a process which ctrlc cannot handle

The ctrlc does handle SIGTERM according to the documentation. Also, no crate gives the power to handle SIGKILL because the kernel never passes that signal onto the process; it just ends the process immediately. Just providing some clarification; the signal-hook crate is a fine choice as well.

@jtroo
Copy link
Owner

jtroo commented Jul 20, 2022

I don't think there's any issue in the repository that needs to be fixed, is that correct? Closing this issue, but feel free to re-open if I'm mistaken.

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

No branches or pull requests

2 participants