Skip to content

fix(layout): a deep stack for the ELK server, no waiting on a layout that overflowed it - #271

Merged
endrix merged 2 commits into
mainfrom
fix/elk-server-stack
Dec 2, 2026
Merged

endrix merged 2 commits into
mainfrom
fix/elk-server-stack

Conversation

@endrix

@endrix endrix commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Found while measuring the layout rates for #270.

The fault

ELK's network simplex walks a graph by recursion (NetworkSimplex.tightTreeDFS). On the megabyte a JVM gives a thread, a warm server overflowed its stack on a deep graph of 10,000 nodes. GzipFramedSocketElkServer catches the StackOverflowError, logs SEVERE: [n] Error: null and leaves the request open. The client then waits out the whole timeout (120 s) before the layout is refused or handed to elkjs.

The fix, on the TypeScript side (the shipped jar is unchanged)

  • A 64 MB stack for the server's threads (ELK_SERVER_STACK, -Xss64m), added to the JVM's arguments unless a stack is named already: -Xss… or -XX:ThreadStackSize=… in <ns>.layout.javaOpts, JAVA_TOOL_OPTIONS or JDK_JAVA_OPTIONS. The memory is reserved, and used only as deep as a layout goes.
  • A server that reports a stack overflow is retired, not killed on the spot. It is shared by every diagram and healthy but for the one request. The next layout starts another server. The old one answers what else it was asked, and is ended when the layouts that overflowed are all it has left (ElkServerProcess.during, which SwitchingElk now asks the server through).
  • The layout that overflowed says why. It fails with the stack as its reason, which reaches the status line of the diagram (a refusal) or the log (a fallback to elkjs), and names JVM options that keep the heap: -Xmx2g -Xss256m.
  • The error stream is read by the line. A line the pipe hands over in pieces is still heard, and the word inside another line is no overflow.

Checked against the real server

Through SwitchingElk and a real ElkServerProcess, a deep graph of 10,000 nodes and a graph of 300 nodes asked for at once, two rounds each:

stack deep graph small graph beside it
-Xss512k (made too small) overflow reported, failed with the stack reason after 3 s, handed to elkjs answered by the server in 1.6 s, then the server was ended, and the next round started another
default, now 64 MB laid out by the server in 5.5 s and 4.7 s answered by the server

Earlier, with -Xss64m given by hand, a warm server laid out 10,000 nodes in 5.4 s and 4.4 s and 20,000 nodes in 9.7 s, where the same warm server on the default stack overflowed. A cold server on a 1 MB stack did lay the 10,000-node graph out (three rounds): the overflow needs a server that has run many layouts.

Not done

A graph that overflows 64 MB costs a server start at each try. The cure is in the server itself (answer the error as it answers a layout error, and keep running), which needs a new build of the jar and its copy in every product.

Checks

  • packages/diagram-server: 148 files, 1,153 tests pass. Type check clean. scripts/check-neutrality.sh passes.

https://claude.ai/code/session_01EToj6NxqLABK3H5cucHNsY

…out that overflowed it

ELK's network simplex walks a graph by recursion. On the megabyte a JVM gives
a thread, a warm server overflowed its stack on a deep graph of 10,000 nodes.
The server catches the error, logs it and never answers the request, so the
layout waited out its whole timeout before it was refused or handed to elkjs.

- The server's threads get a 64 MB stack (`ELK_SERVER_STACK`), unless the JVM
  options name one. With it that graph lays out in five seconds.
- A server that still says a layout overflowed its stack is ended, like one
  out of heap. That fails the layout at once (3.5 s against the real server
  on a stack made too small), and the next layout starts the server again.

Claude-Session: https://claude.ai/code/session_01EToj6NxqLABK3H5cucHNsY
- The server is shared by every diagram, and an overflow leaves it healthy
  but for one request. It is no longer killed on the spot: it is retired (the
  next layout starts another), answers what else it was asked, and is ended
  when the layouts that overflowed are all it has left
  (`ElkServerProcess.during`, which `SwitchingElk` asks the server through).
- A layout that overflowed fails with the reason, on the status line and in
  the log, and with JVM options that keep the heap (`-Xmx2g -Xss256m`).
- The error stream is read by the line: a line the pipe hands over in pieces
  is still heard, and the word inside another line (an id the server quotes)
  is no overflow.
- A stack named in `JAVA_TOOL_OPTIONS` or `JDK_JAVA_OPTIONS` is left alone.

Not done: a graph that overflows 64 MB costs a server start at each try. The
cure is in the server (answer the error, as it answers a layout error), with
the next build of its jar.

Claude-Session: https://claude.ai/code/session_01EToj6NxqLABK3H5cucHNsY
@endrix
endrix merged commit 9a1492d into main Dec 2, 2026
1 check passed
@endrix
endrix deleted the fix/elk-server-stack branch December 2, 2026 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant