Skip to content

Shut down Clojure agents after compilation - #69

Merged
chali merged 1 commit into
nebula-plugins:mainfrom
dgshep:fix-compile-agent-shutdown
Nov 17, 2026
Merged

chali merged 1 commit into
nebula-plugins:mainfrom
dgshep:fix-compile-agent-shutdown

Conversation

@dgshep

@dgshep dgshep commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Problem

ClojureCompile forks a clojure.main JVM to run clojuresque.tasks.compile/main. If loading a namespace during compilation calls send-off (for example, an async logger), Clojure creates non-daemon clojure-agent-send-off-pool threads. Nothing calls shutdown-agents, so after the compiler script finishes, the idle pool keeps the forked JVM alive for its 60-second keepalive. Each affected compile therefore takes about a minute longer than necessary.

Fix

compile.clj wraps the compile/require loop in try/finally and calls (shutdown-agents) in the finally. This covers both successful and failed compiles:

  • Successful compiles still return true, and compiler exceptions still propagate unchanged.
  • The change is limited to the forked compiler driver. Gradle-side dependency analysis and the run/test/doc/repl tasks are unaffected.
  • There is no System/exit and no timeout.

shutdown-agents shuts the executors down in an orderly way, so agent actions already running when compilation ends do finish. One limitation, standard Clojure behavior when exiting a standalone process: actions still queued behind a running action on the same agent are not run after shutdown. Previously the 60 s linger happened to give them time to finish. Code that sends agent work at namespace-load time and needs it to finish should wait for it, for example with await.

Tests

I extended NoToolchainNebulaClojurePluginIntegrationSpec with a test that covers compile and require modes, each on success and on failure. Each case starts a bounded child clojure.main that loads the real compile.clj/util.clj resources through the task's stdin/EDN protocol. The fixture namespace sends an agent action when loaded: the action sleeps 500 ms, then writes a marker file. Failure cases also throw on load.

The test asserts that:

  • the child exits within 20 s;
  • the running action completed;
  • successful cases return true;
  • failure cases surface the original compiler error.

The test JVM's own agent pools are never shut down.

Red/green:

  • With the fix: 4/4 pass.
  • shutdown-agents removed: 4/4 fail because the compiler process doesn't exit within the timeout.
  • shutdownNow on the send-off pool: 4/4 fail because the running action is lost.

./gradlew check: everything passes locally except the existing Java 8 toolchain specs, which fail only because no local Java 8 is installed (CI installs it).

@dgshep
dgshep marked this pull request as ready for review November 16, 2026 22:25
@chali
chali merged commit d5ac7b0 into nebula-plugins:main Nov 17, 2026
3 checks passed
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.

2 participants