Store schedulerThread in StablePtr.#1540
Merged
Merged
Conversation
The scheduler `ThreadId` needs to also persist between `:r` invocations. Otherwise, the CAF gets recreated and the previous scheduler thread (along with its closure) leaks, causing accumulating heap growth over time. Also, the `ComponentState`, in order to be reified reliably between each `:r`, needs to be fetched by unique `Key`. This means for users, when using `live`, they should always `Key` `Component` uniquely. The only `Component` that is allowed to be left unkeyed during hot-reload (w/ `live`) is the top-level root `Component`, of id `1`. - [x] Add `ThreadId` to `StablePtr` in `live` / `reload` (along with `components :: `IORef (CompenentState parent props model action)` - [x] Don't `clearPage` during `live`, just `clearBody`. Scripts / styles only need to be loaded once. - [x] Drop `killThread =<< readIORef schedulerThread` - [x] Add `_componentKey` to `ComponentState`.
Owner
Author
|
Consider constraining |
When using `evalIO` in the playground (`try-miso`), the `GHCi` session is reused and the CAFs in miso lib do not get cleared between `Main.hs` reloads (`schedulerThread` lingers). When using WASM browser mode (GHCi :r mode) the CAFs in miso do get cleared (hence the need for the StablePtr, and C heap storage). So we still need to explicitly kill the Thread in `initComponent` (this logic was previously moved to `live` / `reload`). The `killThread` call on an already dead thread is a no-op (occurs w/ `live` and `reload` functions) but when using `startApp` with `evalIO` (on the playground) it will actually kill the old thread if a reload is detected (`IM.size components > 0`).
`ComponentId` is not a stable identifier across reloads. `Key` is more accurate. Unkeyed Component don't get hot reload benefits. Changing component Key during interactive editing w/ live will still segfault. This can be solved by using a Generic diff.
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.
When using
liveandreloadfunctions, the schedulerThreadIdneeds to persist between:rinvocations (viaStablePtr). Otherwise, the CAF gets recreated and the previous scheduler thread (along with its closure) leaks into the heap (due to theforevercall). This causes accumulated heap growth during a hot reload session. By storing theThreadIdin theStablePtrthis allows us to kill the previous schedulerThreadIdcleanly.Likewise the
ComponentState, in order to be reified reliably, needs to be fetched by uniqueKey(notComponentId, which is not stable between user changes to application state). Onus falls on the user toKeytheirComponentuniquely during a hot-reload session (with the exception of the top levelComponentIdwhich is always 1).This means
liveusers should alwaysKeyComponentuniquely. The onlyComponentthat is allowed to be left unkeyed during hot-reload (w/live) is the top-level rootComponent, of id1.ThreadIdtoStablePtrinlive/reload(along withcomponents :: IORef (CompenentState parent props model action))clearPageduringlive, justclearBody. Scripts / styles only need to be loaded once.killThread =<< readIORef schedulerThread_componentKeytoComponentState.