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

How to ensure single daemon process? #2

Open
tkellogg opened this issue Jan 2, 2022 · 5 comments
Open

How to ensure single daemon process? #2

tkellogg opened this issue Jan 2, 2022 · 5 comments
Labels
stability Things that cause backups to not happen (crash, no start, no watch, etc.)

Comments

@tkellogg
Copy link
Owner

tkellogg commented Jan 2, 2022

Right now there are some safeguards to ensure that only a single daemon process is running.

Consequences of multiple daemons:

  • Possible Git race condition on creating multiple commits
  • Race conditions during logging
  • Possible incomplete (corrupted) reads of the runtime database

Right now, we read the runtime database file at the beginning of each loop iteration. If the pid does not equal the current process' ID, then the process kills itself.

Please comment if you think this algorithm is inadequate.

@tkellogg tkellogg added the stability Things that cause backups to not happen (crash, no start, no watch, etc.) label Jan 2, 2022
@bjorn3
Copy link

bjorn3 commented Jan 3, 2022

I think there is a race condition where two daemons could be started at the same time, read the config file notice that the pid field is notbset and then write to the same repo. Also if the daemon crashes it will leave the pid field set in he config.

The daemon could create a unix socket in the $XDG_RUNTIME_DIR dir. If the socket already exists and is bound to this means another daemon exists and the current process should exit. Else the current process can bind to the socket. Other processed could then connect to the socket to send new repos to watch or perform other commands (maybe you could have a shutdown command or a command to list all currently watched repos) The $XDG_RUNTIME_DIR dir is removed when you log out. With this schema there is no need for any processes but the daemon to read or write the config file and any crashes will result in an unbound socket.

@tkellogg
Copy link
Owner Author

tkellogg commented Jan 4, 2022

@bjorn3 I like this approach. It's clean. Why not use TCP sockets? that way it would work on Windows too.

Does it require any elevated permissions to listen to a socket on localhost? On any OS that you're aware of

@bjorn3
Copy link

bjorn3 commented Jan 4, 2022

A TCP socket only works when there is a single user on the local machine. It requires picking a port that isn't used by any other service. It may get blocked by a firewall (On windows you get a popup the first time it runs AFAIK. I'm not sure if you need admin permission to allow it.) And it may show up in port scans done by websites yoy visit like for example ebay is known to do.

TCP sockets may be a fine alternative when unix domain sockets are unavailable, but I think unix domain sockets are better for things like this. OS portability is not a big deal. Even windows 10 now supports them (not sure if libstd already exposes them on windows. if not you can use winapi)

@tkellogg
Copy link
Owner Author

tkellogg commented Jan 8, 2022

@bjorn3 Another example where this is important is tests. Right now I have a thread::sleep to wait for the server starts. However, in the tests example I can read lines from stdout until I get a certain message (haven't done that yet). It works there because it's a child process. We could probably do the same thing via sockets.

Regarding sockets — my bias is toward going full-fledged HTTP, because you always seem to want more out of stuff like this. Like:

  • GET /status — health checks, basic stats
  • DELETE /status — kill current poller
  • GET /last_commit — get the log entry for the last job
  • GET /next_commit?wait=10 — wait up to 10 seconds for the next commit

I suppose compatibility with other tools (e.g. cURL) isn't terribly important since we can easily control the client side also. Is there a good RPC crate for unix sockets?

@bjorn3
Copy link

bjorn3 commented Jan 8, 2022

If you use http please make sure to check that the Origin header doesn't point to a website to prevent a security hole.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stability Things that cause backups to not happen (crash, no start, no watch, etc.)
Projects
None yet
Development

No branches or pull requests

2 participants