fermyon / enterprise-architectures-and-patterns

Collection of Enterprise Architectures & Patterns built with Spin and WebAssembly
51 stars 12 forks source link

SQLite apps with external libsql provider: stream expired or other invalid connection issues #18

Open vdice opened 4 months ago

vdice commented 4 months ago

When either of the SQLite apps (http-crud-go-sqlite or http-crud-js-sqlite) are run using an external libsql provider (specifically, Turso), things work fine for a spell but then after a little while of inactivity, STREAM_EXPIRED and other errors result.

Not sure if this is specific to the provider (Turso) or the underlying sql libraries, or our use of them. I've repro'd on a local turso db as well as a remote turso db, but the steps below will use a local one.

To repro, launch a local turso db in one terminal and then spin up one of the apps with runtime-config pointing to the db in another eg:

turso dev
[sqlite_database.default]
type = "libsql"
url = "http://127.0.0.1:8080"
token = ""
spin up --sqlite @migrations.sql --runtime-config-file runtime-config.toml

REST requests should work initially, but then after a period of time will fail, eg:

$ curl -X GET http://localhost:3000/items
io error: Hrana: `api error: `{"message":"The stream has expired due to inactivity","code":"STREAM_EXPIRED"}``

$ curl -X GET http://localhost:3000/items
io error: Hrana: `api error: `Received an invalid baton``

Here are logs from a turso db running locally:

$ turso dev
sqld listening on port 8080.
Use the following URL to configure your libSQL client SDK for local development:

    http://127.0.0.1:8080

By default, no auth token is required when sqld is running locally. If you want to require authentication, use --auth-jwt-key-file to specify a file containing the JWT key.

This server is using an ephemeral database. Changes will be lost when this server stops.
If you want to persist changes, use --db-file to specify a SQLite database file instead.
2024-07-18T22:15:29.941091Z ERROR libsql_server::hrana::http: hrana server: Stream handle for 2342824010987366139 is expired
2024-07-18T22:29:49.898140Z ERROR libsql_server::hrana::http: hrana server: Stream handle for 2342824010987366139 was not found
ThorstenHans commented 1 month ago

I was able to repro the issue with Spin 2.7.0. It seams to be fixed in the meantime, as I wasn't able to repro the error with Canary.

Thanks @vdice !

I'll setup a reminder to verify this with the next stable release of Spin as well!