From f402509b62a3839a27ba08de1202143e6ba4550f Mon Sep 17 00:00:00 2001 From: stephan Date: Wed, 30 Jul 2025 14:50:44 +0000 Subject: [PATCH] JS: add a random element when assigning db IDs to Worker1 db instances. This addresses [https://github.com/sqlite/sqlite-wasm/issues/113 | the npm project's ticket #113], which demonstrates that two independent workers can end up generating non-globally-unique IDs. That's not a bug, per se, but it makes the IDs useless for client-side mapping of db connection IDs to client-side state, so we add some randomness to them. FossilOrigin-Name: 6d2a43075fda9c4eae28f9ca4aaf459c41d305c861947a47fb2cbd513249c4b9 --- ext/wasm/api/sqlite3-api-worker1.c-pp.js | 12 ++++++++++-- manifest | 14 +++++++------- manifest.uuid | 2 +- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/ext/wasm/api/sqlite3-api-worker1.c-pp.js b/ext/wasm/api/sqlite3-api-worker1.c-pp.js index 5e088f4384..2b5f2da2c3 100644 --- a/ext/wasm/api/sqlite3-api-worker1.c-pp.js +++ b/ext/wasm/api/sqlite3-api-worker1.c-pp.js @@ -385,10 +385,18 @@ sqlite3.initWorker1API = function(){ const getDbId = function(db){ let id = wState.idMap.get(db); if(id) return id; - id = 'db#'+(++wState.idSeq)+'@'+db.pointer; + id = 'db#'+(++wState.idSeq)+'@'+db.pointer+':'+ + Math.floor(Math.random() * 10000000); /** ^^^ can't simply use db.pointer b/c closing/opening may re-use the same address, which could map pending messages to a wrong - instance. */ + instance. + + 2025-07: https://github.com/sqlite/sqlite-wasm/issues/113 + demonstrates that two Worker1s can end up with the same IDs, + despite using different instances of the library, so we need + to add some randomness to the IDs instead of relying on the + pointer addresses. + */ wState.idMap.set(db, id); return id; }; diff --git a/manifest b/manifest index 03e7aba7f9..b9813a3235 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Enhance\sthe\sprintf\sformatter\sfor\s%#f\ssuch\sthat\sthe\sminus\ssign\sis\somitted\nif\sthe\s'+'\sis\smissing\sand\sall\sdigits\sshown\sin\sthe\srendering\sare\szero.\nThus,\sfor\sexample,\s'%#.2f'\srenders\s-0.004\sas\s"0.00"\sinstead\sof\s"-0.00". -D 2025-07-30T13:37:49.658 +C JS:\sadd\sa\srandom\selement\swhen\sassigning\sdb\sIDs\sto\sWorker1\sdb\sinstances.\sThis\saddresses\s[https://github.com/sqlite/sqlite-wasm/issues/113\s|\sthe\snpm\sproject's\sticket\s#113],\swhich\sdemonstrates\sthat\stwo\sindependent\sworkers\scan\send\sup\sgenerating\snon-globally-unique\sIDs.\sThat's\snot\sa\sbug,\sper\sse,\sbut\sit\smakes\sthe\sIDs\suseless\sfor\sclient-side\smapping\sof\sdb\sconnection\sIDs\sto\sclient-side\sstate,\sso\swe\sadd\ssome\srandomness\sto\sthem. +D 2025-07-30T14:50:44.181 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -644,7 +644,7 @@ F ext/wasm/api/sqlite3-api-cleanup.js 3ac1786e461ada63033143be8c3b00b26b93954066 F ext/wasm/api/sqlite3-api-glue.c-pp.js 0b76510f3650053bac67ca8947cb6ab9d050ad2218118a2e7796dd37be832ffa F ext/wasm/api/sqlite3-api-oo1.c-pp.js 852f2cd6acddbae487fc4f1c3ec952e6c1e2033aa4e6c7091d330d983c87c032 F ext/wasm/api/sqlite3-api-prologue.js 4f1c2a9dc9caf631907766e9872c27d11b255ccae779e8af01c7f8b932817214 -F ext/wasm/api/sqlite3-api-worker1.c-pp.js f646a65257973b8c4481f8a6a216370b85644f23e64b126e7ae113570587c0ab +F ext/wasm/api/sqlite3-api-worker1.c-pp.js 41733b659a6f51438947c3c1e276e5ea48f7b0926904d98537ad2d77a109457f F ext/wasm/api/sqlite3-license-version-header.js 0c807a421f0187e778dc1078f10d2994b915123c1223fe752b60afdcd1263f89 F ext/wasm/api/sqlite3-opfs-async-proxy.js 9654b565b346dc609b75d15337f20acfa7af7d9d558da1afeb9b6d8eaa404966 F ext/wasm/api/sqlite3-vfs-helper.c-pp.js 3f828cc66758acb40e9c5b4dcfd87fd478a14c8fb7f0630264e6c7fa0e57515d @@ -2213,8 +2213,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 1ad0169b022b280bcaaf94a7fa231591be96b514230ab5c98fbf15cd7df842dd F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 575271c3ae53c64784a2aa1e9b7c6cdcb0402022967155dd176a6b1f1e560b50 -R da2d87335cfe87b22f4e2c237c3ffa0c -U drh -Z 40e2e6d0c71b15c3369bf3e06e4b43eb +P 09e1d7c7b4615262dd03adf1be201122f3cb8909ad381d67f51c812f07e25719 +R 23a16f28102ef2a710420716877b61c9 +U stephan +Z eda584cf2bd578a7e7092d276d114db7 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 1327e82bea..1ad1906897 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -09e1d7c7b4615262dd03adf1be201122f3cb8909ad381d67f51c812f07e25719 +6d2a43075fda9c4eae28f9ca4aaf459c41d305c861947a47fb2cbd513249c4b9 -- 2.47.2