Skip to content

feat: serve trace.profiler data via local HTTP server#13530

Merged
Kha merged 2 commits into
leanprover:masterfrom
Kha:push-olsystqtnysy
May 10, 2026
Merged

feat: serve trace.profiler data via local HTTP server#13530
Kha merged 2 commits into
leanprover:masterfrom
Kha:push-olsystqtnysy

Conversation

@Kha
Copy link
Copy Markdown
Member

@Kha Kha commented Apr 26, 2026

This PR adds a trace.profiler.serve option that, when enabled, serves the Firefox Profiler-compatible profile JSON on an ephemeral 127.0.0.1 port and opens https://profiler.firefox.com/from-url/... in the user's default browser, à la samply. The server shuts down once the profile has been fetched.

Compared to trace.profiler.output, which writes the profile to a file that the user must then manually upload to profiler.firefox.com, this is a single-command workflow not touching the file system.

Implementation notes:

  • Std.Http.Server now exposes the bound socket address so the caller can read back the OS-assigned ephemeral port when binding to port 0.
  • addTraceAsMessages and the import-time trace recording now check a shared trace.profiler.isExporting predicate; the previous trace.profiler.output-only check would otherwise consume the trace state before serving could see it.

@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Apr 26, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

mathlib-lean-pr-testing Bot commented Apr 26, 2026

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase a21d3b1ef77e0afcb619d4e245d55ba0a99d6d8a --onto 24bef91f9a20a45f074729e869461d374687de1c. You can force Mathlib CI using the force-mathlib-ci label. (2026-04-26 15:55:48)
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 2d79ec2883f1ff66cbc02d7e872258c7e7dcb932 --onto ea6e76707835317858b7dd36c95322679c50aaac. You can force Mathlib CI using the force-mathlib-ci label. (2026-05-06 15:39:10)

@leanprover-bot
Copy link
Copy Markdown
Collaborator

leanprover-bot commented Apr 26, 2026

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase a21d3b1ef77e0afcb619d4e245d55ba0a99d6d8a --onto 3fc99eef102549c743c6c63547983319bfae6f01. You can force reference manual CI using the force-manual-ci label. (2026-04-26 15:55:50)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 2d79ec2883f1ff66cbc02d7e872258c7e7dcb932 --onto 3fc99eef102549c743c6c63547983319bfae6f01. You can force reference manual CI using the force-manual-ci label. (2026-05-06 15:39:12)

@Kha Kha force-pushed the push-olsystqtnysy branch from e525dca to 342f191 Compare May 6, 2026 13:23
@Kha Kha marked this pull request as ready for review May 6, 2026 13:23
@Kha Kha requested a review from TwoFX as a code owner May 6, 2026 13:23
Comment thread src/Std/Http/Server.lean
Comment on lines +69 to +74
/--
The local socket address the server is bound to. When `serve` is called with a port of `0`, this
reflects the ephemeral port assigned by the OS rather than the requested address.
`none` for servers constructed without an associated listening socket (e.g. `serveConnection`).
-/
localAddr : Option Net.SocketAddress := none
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@algebraic-dev Does this look reasonable?

Comment on lines +67 to +70
let _ ← Async.ofAsyncTask (.ofPurePromise done)
-- The handler signals `doneCh` before the response body is written; brief pause so the writer
-- can drain before `shutdownAndWait` cancels its task.
Async.sleep 200
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@algebraic-dev Probably not avoidable?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now it's not avoidable :S

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an issue we should open?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a quite special use case, not sure other APIs have a solution for that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, after looking more closely at the use case I agree.

Kha added 2 commits May 6, 2026 14:04
This PR adds a `trace.profiler.serve` option that, when enabled, serves the
Firefox Profiler-compatible profile JSON on an ephemeral `127.0.0.1` port and
opens `https://profiler.firefox.com/from-url/...` in the user's default
browser, à la `samply`. The server shuts down once the profile has been
fetched.

Compared to `trace.profiler.output`, which writes the profile to a file that
the user must then manually upload to `profiler.firefox.com`, this is a
single-command workflow.

Implementation notes:
* `Std.Http.Server` now exposes the bound socket address so the caller can
  read back the OS-assigned ephemeral port when binding to port `0`.
* `addTraceAsMessages` and the import-time trace recording now check a
  shared `trace.profiler.isExporting` predicate; the previous
  `trace.profiler.output`-only check would otherwise consume the trace
  state before serving could see it.
* `interpreter.prefer_native` is enabled in stage0 so the new
  `trace.profiler.serve` option getter can be invoked from compile-time
  elaboration in stage1.
@Kha Kha force-pushed the push-olsystqtnysy branch from f439e2c to d013784 Compare May 6, 2026 14:04
@Kha Kha added this pull request to the merge queue May 8, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 8, 2026
@Kha Kha added this pull request to the merge queue May 10, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 10, 2026
@Kha Kha added this pull request to the merge queue May 10, 2026
Merged via the queue into leanprover:master with commit 654017f May 10, 2026
19 checks passed
@Kha Kha deleted the push-olsystqtnysy branch May 11, 2026 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-other toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants