Skip to content

Commit

Permalink
Fix ctrl+c shutdown for serve-doc (#297)
Browse files Browse the repository at this point in the history
When the ctrl+c signal (SIGINT on unix) is received, we fire off a
[`CancellationToken`](https://docs.rs/tokio-util/latest/tokio_util/sync/struct.CancellationToken.html),
which:
- gracefully shuts down the server process
- shuts down the GC task, which triggers:
- shuts down persistence task (which waits until every dirty doc is
persisted to return)

The mechanism for the GC process triggering the shut down of the
persistence task is the `persistence_cancellation_token` which is fired
when the GC task completes. However, in `serve-doc`, we only care about
one doc so we don't GC inactive docs. This means the
`persistence_cancellation_token` never fires in `serve-doc` mode.

The fix is to simplify things: one `cancellation_token` is responsible
for all three graceful shutdowns (server, GC task, and persistence
task). When we cancel the GC task it simply breaks out of the loop, so
there is no code in the GC task that must complete before we shut down
the persistence task. (@rbtying please correct me if I'm missing
something!)
  • Loading branch information
paulgb authored Oct 1, 2024
1 parent ab566bb commit f585e07
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions crates/y-sweet/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Server {
let sync_kv = dwskv.sync_kv();
let checkpoint_freq = self.checkpoint_freq;
let doc_id = doc_id.to_string();
let persistence_cancellation_token = CancellationToken::new();
let cancellation_token = self.cancellation_token.clone();

// Spawn a task to save the document to the store when it changes.
self.doc_worker_tracker.spawn(
Expand All @@ -151,7 +151,7 @@ impl Server {
sync_kv,
checkpoint_freq,
doc_id.clone(),
persistence_cancellation_token.clone(),
cancellation_token.clone(),
)
.instrument(span!(Level::INFO, "save_loop", doc_id=?doc_id)),
);
Expand All @@ -162,8 +162,7 @@ impl Server {
self.docs.clone(),
doc_id.clone(),
checkpoint_freq,
persistence_cancellation_token,
self.cancellation_token.clone(),
cancellation_token,
)
.instrument(span!(Level::INFO, "gc_loop", doc_id=?doc_id)),
);
Expand All @@ -178,7 +177,6 @@ impl Server {
docs: Arc<DashMap<String, DocWithSyncKv>>,
doc_id: String,
checkpoint_freq: Duration,
persistence_cancellation_token: CancellationToken,
cancellation_token: CancellationToken,
) {
let mut checkpoints_without_refs = 0;
Expand Down Expand Up @@ -210,7 +208,6 @@ impl Server {
}
};
}
persistence_cancellation_token.cancel();
tracing::info!("Exiting gc_loop");
}

Expand Down

0 comments on commit f585e07

Please sign in to comment.