SQLite User Forum

SAHPool VFS: How to close the VFS
Login

SAHPool VFS: How to close the VFS

(1) By Matt (Matt_TOTW) on 2025-01-28 17:31:09 [source]

Hello, I am wondering if it is possible to close the sahpool once it has been installed (with installOpfsSAHPoolVfs). For context, what I want to do is to first create and use an in-memory db (with oo1.DB) and then, with user permission, import that into OPFS. Subsequent sessions will use the db in the opfs with the sahpool vfs so my understanding is that I need to use that vfs for the import because of the opaque file names - let me know if I am mistaken on that! The reason I want to close the importing vfs is a) I don't need it as I will continue to use the in-memory db for the duration of the session, and more importantly b) If a session is opened in another tab, I want to be able to use the db that was imported into the opfs. Additional tabs beyond that will use a shared service (as pointed to in the docs, thanks rhashimoto!). Many thanks!

(2) By Stephan Beal (stephan) on 2025-01-28 17:43:36 in reply to 1 [link] [source]

Hello, I am wondering if it is possible to close the sahpool once it has been installed

Yes, but... removing the VFS will also remove all data associated with the VFS. Once activated, there is currently no API for removing/disabling the VFS without also removing the associated data. That seems like a reasonable thing to be able to do, though, so i've noted it here on my local TODO list and will report back in this thread either when it's working or if there's some as-yet-unrecognized hurdle which makes it unfeasible (there is currently no reason to believe that's the case, though).

For completeness's sake, the current ("nuclear option") API can be found by searching this page for "removeVfs".

(3) By Matt (Matt_TOTW) on 2025-01-28 17:57:44 in reply to 2 [link] [source]

Thanks Stephan, it’s as I suspected but it’s great to know you’ll be giving it some attention, thanks! I did find removeVfs and yep, found it to be very nuclear indeed!

(4) By Stephan Beal (stephan) on 2025-01-29 11:38:26 in reply to 2 [link] [source]

... will report back in this thread either when it's working or if there's some as-yet-unrecognized hurdle which makes it unfeasible (there is currently no reason to believe that's the case, though).

There is an experimental version of this in the opfs-sahpool-pause branch and there's a snapshot build on the wasm site which contains this feature. See the diff in that first link for the docs and a test which demonstrates it, but here's a summary:

  • Assuming that ospu is a OpfsSAHPoolUtil instance...
  • ospu.pauseVfs() will (A) close all opened SAH handles and (B) unregister the VFS from SQLite. It throws if any db instance is open with that VFS because the only other alternative would be to close the file handles out from under the db, invoking Undefined Behavior. In other words: the VFS can only be paused if it's not in active use.
  • ospu.isPaused() returns true if the VFS is paused, else false.
  • async ospu.unpauseVfs() will (A) reacquire the SAH handles, (B) re-register the VFS with SQLite, and (C) return a Promise which resolves to ospu.

Would that API help you solve the problem you are trying to solve? If not, suggestions are welcomed. (Similarly, if you have a better name for this feature than "pause," please suggest it.)

Known caveats:

  • This feature requires far more testing before i'm comfortable merging it to trunk for the 3.49 release, but "so far so good."

  • The VFS setup is computationally slow, with its initialization time being directly related to its capacity (O(N)) because it has to asynchronously open each OPFS handle during initialization/unpausing. The higher the VFS's capacity, the longer pausing and unpausing will take.

(5) By Matt (Matt_TOTW) on 2025-01-29 18:01:43 in reply to 4 [link] [source]

This sounds really great, and definitely accomplishes what I was looking for. ‘isPaused’ and ‘unpauseVfs’ are interesting additions. I am not surprised (and fwiw agree) that it should error if a db is open, very sensible and manageable if you ask me. I was expecting that the solution would be to simply tear it down. What I mean by that is that to use it again would require another call to ‘installOpfsSAHPoolVfs’. Consequently, I was expecting the name to be “uninstall” or “close” or similar but the api as you have specified it seems to fit well with “pause”. “stop” could also work and is perhaps a bit more indicative of it releasing its handles. Big thanks and thumbs up from me!!