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(unstable): kv watch should stop when db is closed #21665

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cli/tests/unit/kv_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2248,3 +2248,23 @@ dbTest("set with key versionstamp suffix", async (db) => {
"expected string, number, bigint, ArrayBufferView, boolean",
);
});

Deno.test({
name: "watch should stop when db closed",
async fn() {
const db = await Deno.openKv(":memory:");

const watch = db.watch([["a"]]);
const completion = (async () => {
for await (const _item of watch) {
// pass
}
})();

setTimeout(() => {
db.close();
}, 100);

await completion;
},
});
15 changes: 10 additions & 5 deletions ext/kv/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,21 @@ async fn op_kv_watch_next(
let cancel_handle = resource.cancel_handle.clone();
let stream = RcRef::map(resource, |r| &r.stream)
.borrow_mut()
.or_cancel(db_cancel_handle)
.or_cancel(cancel_handle)
.or_cancel(db_cancel_handle.clone())
.or_cancel(cancel_handle.clone())
.await;
let Ok(Ok(mut stream)) = stream else {
return Ok(None);
};

// doesn't need a cancel handle because the stream ends when the database
// connection is closed
let Some(res) = stream.next().await else {
// We hold a strong reference to `resource`, so we can't rely on the stream
// being dropped when the db connection is closed
let Ok(Ok(Some(res))) = stream
.next()
.or_cancel(db_cancel_handle)
.or_cancel(cancel_handle)
.await
else {
return Ok(None);
};

Expand Down