8000 daemon: handle event queueing and shutdown issues by sandydoo · Pull Request #692 · cachix/cachix · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

daemon: handle event queueing and shutdown issues #692

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

Merged
merged 4 commits into from
May 17, 2025
Merged

Conversation

sandydoo
Copy link
Member
@sandydoo sandydoo commented May 15, 2025
  • The event loop (main message queue) is now larger.
  • sigTERM triggers the immediate shutdown of the daemon.
  • sigINT attempts to gracefully shut down the daemon. Exits immediately if a second signal is sent.
  • The event loop runner can now be short-circuited by the shutdown latch, even if stuck waiting for messages.

sandydoo added 2 commits May 15, 2025 13:36
Use STM to short-circuit reading from the event queue if the latch is
triggered.
@sandydoo sandydoo added the bug Something isn't working label May 15, 2025
@sandydoo sandydoo changed the title Event queue daemon: handle event queueing and shutdown issues May 15, 2025
@domenkozar domenkozar requested a review from Copilot May 15, 2025 13:45
Copy link
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the daemon’s shutdown mechanism by replacing the old MVar‑based exit latch with a new TVar‑based ShutdownLatch and updating the event loop and signal handling accordingly.

  • Replace ExitLatch with ShutdownLatch in the event loop types.
  • Integrate STM‑based ShutdownLatch into the event loop logic for handling shutdown and event processing.
  • Update signal handlers in the daemon to support immediate versus graceful shutdown.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
cachix/src/Cachix/Daemon/Types/EventLoop.hs Removed ExitLatch and updated the data type to use ShutdownLatch.
cachix/src/Cachix/Daemon/Types/Daemon.hs Removed the unused daemonShutdownLatch field.
cachix/src/Cachix/Daemon/Types.hs Added exports/imports for the new event loop error types.
cachix/src/Cachix/Daemon/ShutdownLatch.hs Reimplemented the shutdown latch using TVar with STM support.
cachix/src/Cachix/Daemon/EventLoop.hs Updated event loop logic to integrate with ShutdownLatch for shutdown signaling.
cachix/src/Cachix/Daemon.hs Modified signal handler installation to use the updated shutdown logic.
Comments suppressed due to low confidence (1)

cachix/src/Cachix/Daemon.hs:183

  • [nitpick] The variable 'isSecondInterrupt' holds the previous value of interruptRef; consider renaming it (for example, to 'wasAlreadyInterrupted') to more clearly indicate that a 'True' value means a prior interrupt was already recorded.
isSecondInterrupt <- liftIO $ atomicModifyIORef' interruptRef (True,)

sandydoo added 2 commits May 15, 2025 16:12
Re-use the event loop for shutdown, since that's the main run loop of
the daemon anyway. Remove the unused daemon shutdown latch.

We now also gracefully handle sigINT.
@sandydoo sandydoo marked this pull request as ready for review May 16, 2025 09:48
@domenkozar domenkozar requested a review from Copilot May 17, 2025 11:40
Copy link
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request addresses graceful shutdown and event queue handling issues in the daemon. Key changes include:

  • Updating signal handling to distinguish between immediate and graceful shutdown for sigTERM and sigINT.
  • Refactoring event loop shutdown logic to use a new STM‐based ShutdownLatch and increasing the queue capacity.
  • Adjusting daemon signal handler integration and removing unused shutdown latch fields.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
cachix/src/Cachix/Deploy/Agent.hs Updated signal handler to track and act upon repeated SIGINT events
cachix/src/Cachix/Daemon/Types/EventLoop.hs Replaced exit latch with an STM-based shutdownLatch and related documentation
cachix/src/Cachix/Daemon/Types/Daemon.hs Removed the daemonShutdownLatch field as the shutdown now occurs via the event loop
cachix/src/Cachix/Daemon/Types.hs Updated module imports to include new event loop error definitions
cachix/src/Cachix/Daemon/ShutdownLatch.hs Refactored latch implementation from MVar to TVar with STM functions
cachix/src/Cachix/Daemon/EventLoop.hs Enhanced event loop handling to check for shutdown signals and adjust queue size
cachix/src/Cachix/Daemon.hs Adjusted signal handlers using runInIO for proper Daemon action integration

startExitTimer :: ThreadId -> Daemon ()
startExitTimer mainThreadId = do
void $ liftIO $ forkIO $ do
threadDelay (15 * 1000 * 1000) -- 15 seconds
Copy link
Preview
Copilot AI May 17, 2025

Choose a reason for hiding this comment

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

Consider extracting the magic number for the exit timer delay into a named constant to improve maintainability.

Suggested change
threadDelay (15 * 1000 * 1000) -- 15 seconds
threadDelay exitTimerDelayMicroseconds

Copilot uses AI. Check for mistakes.

queue <- liftIO $ newTBMQueueIO 100
return $ EventLoop {queue, exitLatch}
shutdownLatch <- ShutdownLatch.newShutdownLatch
queue <- liftIO $ newTBMQueueIO 100_000
Copy link
Preview
Copilot AI May 17, 2025

Choose a reason for hiding this comment

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

[nitpick] The increased queue capacity to 100_000 may have performance implications under high load; consider reviewing resource usage and potential memory constraints.

Suggested change
queue <- liftIO $ newTBMQueueIO 100_000
let defaultQueueCapacity = 10_000 -- Default capacity for the event loop queue
queue <- liftIO $ newTBMQueueIO defaultQueueCapacity

Copilot uses AI. Check for mistakes.

@domenkozar domenkozar merged commit ed2c63d into master May 17, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0