Explicitly close realms on shutdown.#5933
Open
michelinewu wants to merge 2 commits into
Open
Conversation
BundleMonFiles updated (1)
Unchanged files (3)
Total files change +467B 0% Final result: ✅ View report in BundleMon website ➡️ |
| obs.NodeObs.RemoveVolmeterCallback(); | ||
| obs.NodeObs.OBS_service_removeCallback(); | ||
| obs.IPC.disconnect(); | ||
| this.realmService.close(); |
Collaborator
There was a problem hiding this comment.
This only closes the worker window's Realm instance. To fully fix the stale-reader/unclean-close risk described in the PR, shutdown needs to close Realm in every renderer that opened it, or move the connection so only the worker opens it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Close Realm Databases on Shutdown
Issues
RealmServiceopens two Realm databases at startup viaconnect():persistentDb— file-backed LMDB database (persistent.realm, schema version 3)ephemeralDb— in-memory Realm (ephemeral.realm)The class had no
close(),shutdown(), ordestroy()method. Neither database was ever explicitly closed. The shutdown sequence inAppServicemade no attempt to close either instance.Realm uses LMDB (Lightning Memory-Mapped Database) internally. LMDB maps database files into the process's virtual address space via
mmapon macOS and a memory-mapped section object on Windows. LMDB is crash-safe by design and the copy-on-write structure means the data file itself is not corrupted by an unclean close. The risk is the stale reader entry on next startup and the possibility of a discarded in-flight write transaction.Fixes
Added
close()toRealmServiceand calledthis.realmService.close()inAppService.shutdownHandler()afterobs.IPC.disconnect()and beforecrashReporterService.endShutdown(), making it the last local resource released before the shutdown complete signal is sent.Files changed:
app/services/realm.ts,app/services/app/app.tsImplications of Not Fixing
Stale reader lock entry. LMDB maintains a reader lock table in a separate
.lockfile. Each open Realm connection registers an entry in this table keyed by PID. When the process exits without callingRealm.close(), the entry is not removed. On the next startup, LMDB must scan the reader table for dead PIDs and clean up stale entries before the environment can be opened. This adds latency to every app launch following an unclean exit and is a known source ofMDB_READERS_FULLerrors in environments with many restart cycles.In-flight write transaction discarded.
Realm.close()commits or rolls back any open write transaction before releasing the environment. If the process exits without closing, any write that was in-progress at the moment of shutdown is silently discarded. Whether this is a real risk depends on whether any write is ever left open across an async boundary where the shutdown IPC could arrive but the behavior on unclean exit is silent data loss with no error.Page flush not guaranteed.
Realm.close()callsmdb_env_close()internally, which flushes dirty pages from the OS page cache to disk viamsync/FlushViewOfFile. Without this call, dirty pages are left to the OS to flush at its discretion. On a system under memory pressure, this increases the window during which committed data that has not yet reached physical storage is at risk.Performance Implications
mmaponprocess exitPIDs after unclean exitmsync/FlushViewOfFilecalled by LMDB close