Skip to content

v3: a Frame is retained per conditional switch for the life of a session, and the last 10 closed sessions stay reachable through transit-java's handler cache #79

Open

Description

@durantschoon

Versions

  • com.hyperfiddle/electric v3-alpha-SNAPSHOT as resolved on 2026-11-21 (electric-v3-alpha-20260519.115706-45.jar; its sources are byte-identical to v3-alpha-ca05cbc93), missionary b.46
  • electric3-starter-app @ 1a18416, release build only: clojure -X:build:prod build-client, then clojure -M:prod -m prod
  • JDK 25.0.2 (G1), Jetty 12.1.0, Chromium 156 (headless), Linux

How the numbers were taken

Server: jcmd <pid> GC.run, then jcmd <pid> GC.class_histogram, counting live
hyperfiddle.electric.impl.runtime3.Frame and all hyperfiddle.* instances.
Browser: CDP HeapProfiler.collectGarbage twice, then Memory.getDOMCounters.
"Closed" means the page was closed and the server had logged WS close.

1. Closed sessions stay reachable on the server (bounded: the last 10)

Unmodified starter app. Each session: open the page, wait for server time: …, close
the page, wait for WS close + 10 s, sample.

after closed session live runtime3.Frame live hyperfiddle.*
boot 0 1,289
1 32 2,281
2 64 3,273
… (+32 / +992 each, exactly)
10 320 11,209
11, 12, 13, 14 320 11,209

Waiting 90 s instead of 10 s after each close gives the same counts (3 sessions tried).

A heap dump taken with three sessions closed shows one holder. Shortest path from a GC
root to a closed session's ring_ws_handler listener closures:

class com.cognitect.transit.impl.ReaderFactory
  static handlerCache            com.cognitect.transit.impl.Cache   (a LinkedHashMap; removeEldestEntry at size > 10)
  .key                           clojure.lang.PersistentHashMap     (the custom read-handler map)
  …                              cognitect.transit$read_handler$reify
  .from_rep                      hyperfiddle.electric.impl.runtime3$socket_reader$fn
  .socket                        Object[24]                         (the socket)
  [2]                            missionary.impl.Propagator$7
  …                              hyperfiddle.electric.impl.runtime3$root_socket$fn
  .subject                       hyperfiddle.electric_ring_adapter3$subject_at$fn
  …                              hyperfiddle.electric_ring_adapter3.RingSocket
  .socket → .session             org.eclipse.jetty.ee9.websocket.common.WebSocketSession
  …                              ring.adapter.jetty$websocket_listener$reify → the listener fns

WriterFactory.handlerCache holds the same sessions through the socket_writer handlers.
If those two static fields are ignored when walking the dump, none of the 96 Frames and
none of the 24 listener closures of the three closed sessions is reachable any more, so
these caches are the only thing keeping them.

What seems to cause it: socket-reader and socket-writer (runtime3.cljc, around lines
1571–1676) build a fresh handler map per socket whose handler fns close over socket,
and pass it to t/reader / t/writer. transit-java caches the merged handler map keyed
by the custom handler map, in a static 10-entry cache per factory, so each connection's
socket (and everything reachable from it) lives until ten newer connections have pushed
it out.

Impact: bounded in count, but each retained session is as large as it grew while open
(see 2), and a quiet server keeps it indefinitely.

2. Within a session, every conditional switch retains a Frame

Measured in a minimal project of ours against the same jar (not inside the starter app).
!key is a namespace-level atom flipped between :a and :b by a plain (non-Electric)
click listener; one cycle is a → b → a. The components below are trimmed for reading: the
measured ones also render the two toggle buttons and some data- attributes.

(e/defn R0IfPure []   ; retains
  (e/client
    (let [k (e/watch !key)]
      (dom/div
        (dom/p (dom/text (if (= k :a) "branch a" "branch b")))))))

(e/defn R0None []     ; does not retain
  (e/client
    (let [k (e/watch !key)]
      (dom/div
        (dom/p (dom/text (str "branch " (name k))))))))

Per cycle, over 10 cycles, server side unless noted:

program Frames hyperfiddle.* browser DOM-counter nodes
R0None (no conditional) 0 0 0
R0IfPure (if between two strings) +2 +18 0
if between a div and a span, each with a p +18 +622 +6
same job with e/for over (e/diff-by identity [k]), e/for-by, or two e/Whens +20 +640 to +670 +6
both branches kept mounted, inactive one hidden, per-branch choices made in plain cljs fns 0 0 0

All series are linear (the same increment every cycle). Nothing is released while the
session is open; after it closes, finding 1 applies.

From reading runtime3.cljc (not instrumented): socket-slot-shared is initialised to
{} (line 1761) and only ever assoc'd (lines 1587 and 1651), and the bodies of
socket-local-frame-up / -down / -ack and socket-remote-frame-up / -down
(lines 1081–1104) are commented out with #_.

Questions

  • Is the per-switch retention the known DOM leak mentioned in the Clojurians channel, and
    is frame release planned for v3?
  • For the closed sessions: would clearing the socket's slots on cancel, or handlers that
    do not close over the socket, be acceptable?

The measurement harness and the heap-dump path script are available if useful.

Activity

  1. dustingetz commented on Nov 21, 2026

    @dustingetz
    Member

    Hello, electric v3 still has some correctness issues. We're working on v4 which is another rewrite, there's a note about that in the slack channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions