Skip to content

fix: refcounting and error reporting in the libuv bindings - #14796

Open
algebraic-dev wants to merge 1 commit into
masterfrom
sofia/libuv-foreach
Open

fix: refcounting and error reporting in the libuv bindings#14796
algebraic-dev wants to merge 1 commit into
masterfrom
sofia/libuv-foreach

Conversation

@algebraic-dev

Copy link
Copy Markdown
Member

This PR fixes reference count, mark_mt and error messages in libuv modules.

@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 Aug 15, 2026
@leanprover-bot

Copy link
Copy Markdown
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-08-14 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-08-15 02:18:37)

@github-actions github-actions Bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Aug 15, 2026
algebraic-dev added a commit that referenced this pull request Aug 15, 2026
Stacks this branch on #14796 -> #14795 -> #14793.

The `timer_stop`/`signal_stop` conflicts keep this branch's shape: the loop only holds a reference
on a handle while it owes a promise, so the release of the promise and of the handle are gated
together on the state check that #14793 introduced.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@mathlib-lean-pr-testing

Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Aug 15, 2026
@TwoFX

TwoFX commented Aug 17, 2026

Copy link
Copy Markdown
Member

Here are two Codex-generated regression tests for a subset of the issues fixed in this PR if you want to include them:

module

import Std.Internal.UV.Signal

/-!
Tests that a signal handler can retry after libuv rejects starting it.
-/

open Std.Internal.UV

def startFails (signal : @& Signal) : IO Bool := do
  try
    discard signal.next
    return false
  catch _ =>
    return true

def test : IO (Bool × Bool) := do
  let signal ← Signal.mk 0 false
  let first ← startFails signal
  let second ← startFails signal
  signal.cancel
  return (first, second)

/-- info: (true, true) -/
#guard_msgs in
#eval test
module

import Std.Async.Basic
import Std.Internal.UV.TCP

/-!
Tests the error reported when a TCP shutdown is requested while one is already pending.
-/

open Std Async Net Internal.UV

def blockPromise (promise : IO.Promise (Except IO.Error α)) : IO α :=
  AsyncTask.ofPromise promise |>.block

partial def recvBytes (socket : TCP.Socket) (remaining : Nat) : IO Unit := do
  if remaining == 0 then
    return
  let some chunk ← blockPromise (← socket.recv? remaining.toUInt64)
    | throw <| IO.userError "unexpected end of stream"
  recvBytes socket (remaining - chunk.size)

def test : IO String := do
  let server ← TCP.Socket.new
  server.bind <| SocketAddressV4.mk (.ofParts 127 0 0 1) 0
  server.listen 1
  let addr ← server.getSockName

  let acceptPromise ← server.accept
  let client ← TCP.Socket.new
  let connectPromise ← client.connect addr
  blockPromise connectPromise
  let accepted ← blockPromise acceptPromise

  let size := 8 * 1024 * 1024
  let payload := ByteArray.mk <| Array.replicate size 0
  let sendPromise ← client.send #[payload]
  let shutdownPromise ← client.shutdown
  let message ← try
    discard client.shutdown
    pure "no error"
  catch
    | .otherError _ message => pure message
    | error => pure s!"unexpected error: {error}"

  recvBytes accepted size
  blockPromise sendPromise
  blockPromise shutdownPromise
  return message

/-- info: "shutdown already in progress" -/
#guard_msgs in
#eval test

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

Labels

builds-mathlib CI has verified that Mathlib builds against this PR changelog-library Library mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN 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.

3 participants