denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.31k stars 5.36k forks source link

Detect server restart with --watch ? #25609

Open bartlomieju opened 1 month ago

bartlomieju commented 1 month ago

Discussed in https://github.com/denoland/deno/discussions/25601

Originally posted by **alexgleason** September 12, 2024 I am trying to call `pglite.close()` before the process exits to avoid corrupting my database, but every time I edit my code with `--watch` it gets corrupted and I have to delete/recreate my database. I used `Deno.addSignalListener('SIGINT', ...)` to clean up (and also added `SIGTERM`, `SIGHUP`, etc), but I can't figure out how to detect **when the server will restart due to a code change when using --watch**. I need to also call my cleanup function in that case. I think? I'm not completely sure if this is the issue or I'm running into a different bug. Any ideas?
alexgleason commented 1 month ago

I think my real issue is that pglite still isn't working correctly. But it's an intermittent issue, so I'm making potentially wrong assumptions about what's causing it. Therefore I don't think detecting reload with --watch is actually needed.

So the real issue is: pglite goes into an unrecoverable state, and I get this error when I try to start it:

RuntimeError: Aborted(). Build with -sASSERTIONS for more info.
    at abort (file:///home/alex/.cache/deno/npm/registry.npmjs.org/@soapbox.pub/pglite/0.2.10/dist/postgres.js:740:11)
    at __abort_js (file:///home/alex/.cache/deno/npm/registry.npmjs.org/@soapbox.pub/pglite/0.2.10/dist/postgres.js:7244:7)
    at abort (wasm://wasm/postgres.wasm-02856ac6:1:7189834)
    at errfinish (wasm://wasm/postgres.wasm-02856ac6:1:2120762)
    at InitWalRecovery (wasm://wasm/postgres.wasm-02856ac6:1:5426759)
    at StartupXLOG (wasm://wasm/postgres.wasm-02856ac6:1:5273250)
    at InitPostgres (wasm://wasm/postgres.wasm-02856ac6:1:625849)
    at AsyncPostgresSingleUserMain (wasm://wasm/postgres.wasm-02856ac6:1:2510013)
    at pg_initdb (wasm://wasm/postgres.wasm-02856ac6:1:3412565)
    at Object.Module._pg_initdb (file:///home/alex/.cache/deno/npm/registry.npmjs.org/@soapbox.pub/pglite/0.2.10/dist/postgres.js:10397:109)

The InitWalRecovery is what catches my eye. I am guessing the WAL is getting corrupted. It happens during live reload, but it also happens after I Ctrl+C and star it again. I don't know if this is even a Deno bug.

lucacasonato commented 1 month ago

@alexgleason You should be able to do addEventListener("unload", () => pglite.close()), I think.

shmup commented 4 days ago

@lucacasonato that works with ^c but not when it detects a file change and restarts, re: --watch