From e577ed5381751adb99943d3cd11aca116126ef95 Mon Sep 17 00:00:00 2001 From: Hexagon Date: Thu, 30 May 2024 21:52:14 +0200 Subject: [PATCH] Fix for prompt totally blocking the event loop --- CHANGELOG.md | 6 ++++++ db.db | Bin 0 -> 24 bytes deno.json | 2 +- src/cli/loop.ts | 10 +++++++++- src/lib/kv.ts | 4 +++- 5 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 db.db diff --git a/CHANGELOG.md b/CHANGELOG.md index 15944f1..6784652 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.15.4 + +- Fix for `prompt` totally blocking the event loop in the cli tool, preventing + synchronization. +- Make the watchdog slightly more invincible. + ## 0.15.3 - Internal fix; Always unblock sync even if a vacuum fails diff --git a/db.db b/db.db new file mode 100644 index 0000000000000000000000000000000000000000..7cc5ed7d2660b719bdb04a18f526d655bc495bf7 GIT binary patch literal 24 ccmZ?v4s&rbFf?_l__N_HM*@RG0RtES0AJ_^YybcN literal 0 HcmV?d00001 diff --git a/deno.json b/deno.json index c263cdc..f4998de 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@cross/kv", - "version": "0.15.3", + "version": "0.15.4", "exports": { ".": "./mod.ts", "./cli": "./src/cli/mod.ts" diff --git a/src/cli/loop.ts b/src/cli/loop.ts index a282df3..13086aa 100644 --- a/src/cli/loop.ts +++ b/src/cli/loop.ts @@ -14,12 +14,17 @@ export function registerCommand(command: string, handler: KVCliHandler) { export async function main() { const container: KVDBContainer = {}; let exit = false; - while (!exit) { + // Collect user input const command = await prompt(Colors.blue(">")); if (command === null) { continue; } + + // Yield to event loop, allowing any scheduled tasks to be executed + await new Promise((resolve) => setTimeout(resolve, 0)); + + // Handle the user input const commandSplit = command.split(" "); const cmd = commandSplit[0].toLowerCase(); const startTime = performance.now(); // Start measuring time @@ -42,5 +47,8 @@ export async function main() { } else { console.error(Colors.red("Invalid command.\n")); } + + // Yield to event loop, allowing any scheduled tasks to be executed + await new Promise((resolve) => setTimeout(resolve, 0)); } } diff --git a/src/lib/kv.ts b/src/lib/kv.ts index 0176635..2a0acbb 100644 --- a/src/lib/kv.ts +++ b/src/lib/kv.ts @@ -276,7 +276,9 @@ export class KV extends EventEmitter { this.watchdogTimer = setTimeout( async () => { // Make sure current run is done - await this.watchdogPromise; + try { + await this.watchdogPromise; + } catch (_e) { /* Ignore */ } // Initiate a new run this.watchdogPromise = this.watchdog();