perf: stop awaiting each socket write in the HTTP server
diff --git a/src/Std/Http/Server/Connection.lean b/src/Std/Http/Server/Connection.lean
index efd093d..a401c0b 100644
--- a/src/Std/Http/Server/Connection.lean
+++ b/src/Std/Http/Server/Connection.lean
@@ -9,6 +9,7 @@
 public import Std.Async.TCP
 public import Std.Async.ContextAsync
 public import Std.Http.Transport
+public import Std.Http.Transport.Outbox
 public import Std.Http.Protocol.H1
 public import Std.Http.Server.Config
 public import Std.Http.Server.Handler
@@ -21,6 +22,7 @@
 
 open Std Async TCP Protocol
 open Time
+open Internal (Outbox)
 
 /-!
 # Connection
@@ -75,6 +77,7 @@
   | bodyInterest (x : Bool)
   | response (x : (Except IO.Error (Response β)))
   | timeout
+  | keepAliveTimeout
   | shutdown
   | close
 
@@ -125,7 +128,7 @@
       else
         selectables := selectables.push (.case (← Selector.sleep sources.timeout) (fun _ => pure .timeout))
     else
-      selectables := selectables.push (.case (← Selector.sleep sources.timeout) (fun _ => pure .close))
+      selectables := selectables.push (.case (← Selector.sleep sources.timeout) (fun _ => pure .keepAliveTimeout))
 
   if let some responseBody := sources.responseBody then
     selectables := selectables.push (.case (Body.recvSelector responseBody) (Recv.responseBody · |> pure))
@@ -398,7 +401,7 @@
     else
       return (state, false)
 
-  | .close => return (state, true)
+  | .close | .keepAliveTimeout => return (state, true)
 
   | .timeout =>
     Handler.onFailure handler "request header timeout"
@@ -470,9 +473,26 @@
   }
 
 /--
+Whether processing `event` may run handler code that must not run after an earlier write failed:
+the handler of a request with an unsafe method, or `Handler.onContinue`. Pipelined requests with
+safe methods may be processed before the earlier responses are written (RFC 9112 §9.3.2).
+-/
+private def needsWrittenOutput : H1.Event .receiving → Bool
+  | .endHeaders head => !head.method.isSafe
+  | .«continue» => true
+  | _ => false
+
+/--
+Reports a failed write of `outbox` to the handler, once.
+-/
+private def reportWriteFailure [Handler σ] (handler : σ) (outbox : Outbox α) : Async Unit := do
+  if let some err ← outbox.takeError? then
+    Handler.onFailure handler err
+
+/--
 Runs the main request/response processing loop for a single connection.
 Drives the HTTP/1.1 state machine through four phases each iteration:
-send buffered output, process H1 events, dispatch pending requests, poll for I/O.
+hand output to the outbox, process H1 events, dispatch pending requests, poll for I/O.
 -/
 private def handle
     {σ : Type} [Transport α] [h : Handler σ]
@@ -500,16 +520,21 @@
     pendingHead := none
   }
 
+  let outbox ← Outbox.new socket
+
   while ¬state.machine.halted do
 
-    -- Phase 1: advance the state machine and flush any output.
+    -- Phase 1: advance the state machine and hand its output to the outbox.
     let (newMachine, step) := state.machine.step
     state := { state with machine := newMachine }
 
-    if step.output.size > 0 then
-      try Transport.sendAll socket step.output.data
-      catch e =>
-        Handler.onFailure handler e
+    unless ← outbox.push step.output do
+      reportWriteFailure handler outbox
+      break
+
+    if step.events.any needsWrittenOutput then
+      unless ← outbox.awaitWritten do
+        reportWriteFailure handler outbox
         break
 
     -- Phase 2: process all events emitted by this step.
@@ -523,17 +548,25 @@
       state := { state with requiresData := false }
       let sources ← buildPollSources socket connectionContext state
       let event ← pollNextEvent config handler sources
+      -- The connection is not idle while the peer still reads the response, so the keep-alive
+      -- timeout starts over once the response is written.
+      if event matches .keepAliveTimeout then
+        if ← outbox.writing then
+          outbox.awaitIdle
+          continue
       let (newState, shouldClose) ← handleRecvEvent handler config event state
       state := newState
       if shouldClose then break
 
-  -- Clean up: close all open streams and the socket.
+  -- Clean up: close all open streams, finish writing, and close the socket.
   if ¬(← Body.isClosed state.requestStream) then
     Body.close state.requestStream
 
   if let some res := state.respStream then
     if ¬(← Body.isClosed res) then Body.close res
 
+  outbox.awaitIdle
+  reportWriteFailure handler outbox
   Transport.close socket
 
 end Connection
diff --git a/src/Std/Http/Transport.lean b/src/Std/Http/Transport.lean
index bce14c8..5c04260 100644
--- a/src/Std/Http/Transport.lean
+++ b/src/Std/Http/Transport.lean
@@ -51,7 +51,19 @@
 
 instance : Transport Socket.Client where
   recv client expect := client.recv? expect
-  sendAll client data := client.sendAll data
+  sendAll client data := do
+    let promise ← client.native.send data
+    -- A finished send needs no await, and a pending one is mapped on the thread that completes it
+    -- instead of in a task of its own.
+    if ← IO.hasFinished promise.result? then
+      match promise.result?.get with
+      | some (.ok ()) => pure ()
+      | some (.error e) => throw e
+      | none => throw (IO.userError "the promise linked to the Async was dropped")
+    else
+      Async.ofAsyncTask <| promise.result?.map (sync := true) fun
+        | none => .error (IO.userError "the promise linked to the Async was dropped")
+        | some res => res
   recvSelector client expect := client.recvSelector expect
 
 namespace Internal
diff --git a/src/Std/Http/Transport/Outbox.lean b/src/Std/Http/Transport/Outbox.lean
new file mode 100644
index 0000000..bddfc4a
--- /dev/null
+++ b/src/Std/Http/Transport/Outbox.lean
@@ -0,0 +1,196 @@
+/-
+Copyright (c) 2026 Lean FRO, LLC. All rights reserved.
+Released under Apache 2.0 license as described in the file LICENSE.
+Authors: Sofia Rodrigues
+-/
+module
+
+prelude
+public import Std.Http.Transport
+
+public section
+
+/-!
+# Outbox
+
+An `Outbox` writes a connection's output to its transport in order, without the connection waiting
+for each write.
+-/
+
+namespace Std.Http.Internal
+
+open Std Async
+
+set_option linter.all true
+
+/--
+The state of an `Outbox`.
+-/
+structure Outbox.State where
+  /--
+  Output handed over while a write was in flight, newest first, written together once it
+  completes. A list, because the state is shared with the thread that completes the write, so an
+  array here would be copied on every append.
+  -/
+  queued : List (Array ByteArray) := []
+
+  /--
+  The size of `queued` in bytes.
+  -/
+  queuedBytes : Nat := 0
+
+  /--
+  A write is in flight.
+  -/
+  writing : Bool := false
+
+  /--
+  A write failed, so later output is dropped.
+  -/
+  failed : Bool := false
+
+  /--
+  The error of the failed write, until it is taken.
+  -/
+  error : Option IO.Error := none
+
+  /--
+  Resolved once no write is in flight.
+  -/
+  idle : Option (IO.Promise Unit) := none
+
+/--
+Writes a connection's output to `socket` in order without the connection waiting for it. At most
+one write is in flight. Output handed over meanwhile goes out in a single write once that write
+completes, started by the thread that completed it.
+-/
+structure Outbox (α : Type) where
+  private mk ::
+
+  /--
+  The transport the output is written to.
+  -/
+  socket : α
+
+  private state : IO.Ref Outbox.State
+
+namespace Outbox
+
+/--
+Creates an outbox that writes to `socket`.
+-/
+def new (socket : α) : BaseIO (Outbox α) :=
+  return { socket, state := ← IO.mkRef {} }
+
+/--
+Joins queued output, given newest first, into one write in order.
+-/
+private def joinQueued : List (Array ByteArray) → Array ByteArray
+  | [batch] => batch
+  | batches => batches.foldr (fun batch acc => acc ++ batch) #[]
+
+/--
+Starts writing `data`, then whatever was queued meanwhile once the write completes. For a socket,
+the next write thus starts on the event loop's own thread.
+-/
+private partial def write [Transport α] (outbox : Outbox α) (data : Array ByteArray) :
+    BaseIO Unit := do
+  let task ← EAsync.toBaseIO (Transport.sendAll outbox.socket data)
+  BaseIO.chainTask (sync := true) task fun result => do
+    let (next, idle) ← outbox.state.modifyGet fun s =>
+      match result with
+      | .error err =>
+        ((#[], s.idle), { s with
+          queued := [], queuedBytes := 0, writing := false, failed := true, error := some err
+          idle := none })
+      | .ok () =>
+        if s.queued.isEmpty then ((#[], s.idle), { s with writing := false, idle := none })
+        else ((joinQueued s.queued, none), { s with queued := [], queuedBytes := 0 })
+    if let some idle := idle then
+      idle.resolve ()
+    unless next.isEmpty do
+      outbox.write next
+
+/--
+Hands `buffer` to the outbox, which writes it after everything handed over before. Returns how many
+bytes now wait behind the write in flight, or `none` if a write failed and `buffer` was dropped.
+-/
+def send [Transport α] (outbox : Outbox α) (buffer : ChunkedBuffer) : BaseIO (Option Nat) := do
+  if buffer.size = 0 then
+    return if (← outbox.state.get).failed then none else some 0
+  let data := buffer.data
+  let (start, queued?) ← outbox.state.modifyGet fun s =>
+    if s.failed then ((false, none), s)
+    else if s.writing then
+      let queuedBytes := s.queuedBytes + buffer.size
+      ((false, some queuedBytes), { s with queued := data :: s.queued, queuedBytes })
+    else ((true, some 0), { s with writing := true })
+  if start then
+    outbox.write data
+    -- A write that fails without suspending has already dropped `buffer`.
+    if (← outbox.state.get).failed then
+      return none
+  return queued?
+
+/--
+Returns `true` while a write is in flight.
+-/
+def writing (outbox : Outbox α) : BaseIO Bool :=
+  return (← outbox.state.get).writing
+
+/--
+A task that finishes once no write is in flight, or `none` if none is.
+-/
+def idle? (outbox : Outbox α) : BaseIO (Option (Task (Option Unit))) := do
+  unless (← outbox.state.get).writing do
+    return none
+  let fresh ← IO.Promise.new
+  outbox.state.modifyGet fun s =>
+    if !s.writing then (none, s)
+    else match s.idle with
+      | some idle => (some idle.result?, s)
+      | none => (some fresh.result?, { s with idle := some fresh })
+
+/--
+Waits until no write is in flight.
+-/
+def awaitIdle (outbox : Outbox α) : Async Unit := do
+  if let some idle ← outbox.idle? then
+    BaseAsync.ofTask (Task.map (sync := true) (fun _ => (Except.ok () : Except IO.Error Unit)) idle)
+
+/--
+Returns `true` once a write failed.
+-/
+def failed (outbox : Outbox α) : BaseIO Bool :=
+  return (← outbox.state.get).failed
+
+/--
+Waits until every write handed over so far completed. Returns `false` if a write failed.
+-/
+def awaitWritten (outbox : Outbox α) : Async Bool := do
+  outbox.awaitIdle
+  return !(← outbox.failed)
+
+/--
+Takes the error of the failed write, so that only the first caller gets it.
+-/
+def takeError? (outbox : Outbox α) : BaseIO (Option IO.Error) :=
+  outbox.state.modifyGet fun s => (s.error, { s with error := none })
+
+/--
+Hands `buffer` to the outbox, then waits for its writes if more than `limit` bytes are queued, so a
+peer that does not read holds up the caller instead of growing its memory. Returns `false` if a
+write failed.
+-/
+def push [Transport α] (outbox : Outbox α) (buffer : ChunkedBuffer) (limit : Nat := 65536) :
+    Async Bool := do
+  match ← outbox.send buffer with
+  | some queued =>
+    if queued ≤ limit then
+      return true
+    outbox.awaitWritten
+  | none => return false
+
+end Outbox
+
+end Std.Http.Internal
diff --git a/tests/async/elab/async_http_tcp_write_path.lean b/tests/async/elab/async_http_tcp_write_path.lean
new file mode 100644
index 0000000..1193721
--- /dev/null
+++ b/tests/async/elab/async_http_tcp_write_path.lean
@@ -0,0 +1,214 @@
+import Std.Http.Test.Helpers
+import Std.Net.Addr
+
+/-!
+Write-path tests over loopback TCP: response order for pipelined requests and streamed bodies, a
+reader that starts late, a write that fails, which must close the connection without running the
+handler of the pipelined `POST` behind it, a stalled streamed body, a client that leaves
+mid-stream, and a keep-alive timeout that must not run while the peer still reads the response.
+-/
+
+open Std.Async
+open Std Http Internal Test
+open Std.Net
+
+def connectPair : Async (TCP.Socket.Client × TCP.Socket.Client) := do
+  let server ← TCP.Socket.Server.mk
+  server.bind (SocketAddressV4.mk (.ofParts 127 0 0 1) 0)
+  server.listen 128
+  let addr ← server.getSockName
+  let client ← TCP.Socket.Client.mk
+  client.connect addr
+  let conn ← server.accept
+  return (client, conn)
+
+def readAll (client : TCP.Socket.Client) : Async ByteArray := do
+  let mut out := ByteArray.empty
+  repeat
+    match ← client.recv? 65536 with
+    | none => break
+    | some c => out := out ++ c
+  return out
+
+/-- The text between each `open` and the next `close`, in order. -/
+def tokens (s : String) (open_ close : String) : List String :=
+  (s.splitOn open_).drop 1 |>.map fun p => (p.splitOn close).headD ""
+
+def recordingHandler (failures : IO.Ref (Array String))
+    (f : Request Body.Stream → ContextAsync (Response Body.Any)) : Server.StatelessHandler :=
+  { onRequest := f, onFailure := fun e => failures.modify (·.push (toString e)) }
+
+def withDeadline [Inhabited α] (name : String) (ms : Nat) (x : Async α) : Async α :=
+  Async.race x (do sleep (.ofNat ms); throw <| IO.userError s!"{name}: no result after {ms} ms")
+
+def config : Config := { defaultConfig with maxRequests := 1000 }
+
+def expectNoFailures (failures : IO.Ref (Array String)) : IO Unit := do
+  let fs ← failures.get
+  unless fs.isEmpty do throw <| IO.userError s!"unexpected failures: {fs}"
+
+#eval runGroup "pipelined responses keep their order" <| Async.block do
+  let (client, conn) ← connectPair
+  let n := 300
+  let mut raw := ""
+  for i in [0:n] do raw := raw ++ mkGet s!"/r{i}"
+  raw := raw ++ mkGetClose s!"/r{n}"
+  let failures ← IO.mkRef #[]
+  let h := recordingHandler failures fun req => Response.ok |>.text s!"<{req.line.uri}>"
+  let serve ← async (Server.serveConnection conn h config |>.run)
+  let send ← async do client.send raw.toUTF8; client.shutdown
+  let res ← withDeadline "pipelined" 20000 (readAll client)
+  await send
+  await serve
+  assertResponseCount res (n + 1)
+  let got := tokens (String.fromUTF8! res) "<" ">"
+  unless got == (List.range (n + 1)).map (s!"/r{·}") do
+    throw <| IO.userError s!"responses out of order: {got.take 10}"
+  expectNoFailures failures
+
+#eval runGroup "streamed chunks and the next response keep their order" <| Async.block do
+  let (client, conn) ← connectPair
+  let raw := mkGet "/stream" ++ mkGet "/tail" ++ mkGetClose "/tail"
+  let failures ← IO.mkRef #[]
+  let h := recordingHandler failures fun req =>
+    if toString req.line.uri == "/stream" then
+      Response.ok |>.stream fun s => do
+        for i in [0:500] do
+          s.send (Chunk.ofByteArray s!"[{i}]".toUTF8)
+    else
+      Response.ok |>.text "[tail]"
+  let serve ← async (Server.serveConnection conn h config |>.run)
+  let send ← async do client.send raw.toUTF8; client.shutdown
+  let res ← withDeadline "streamed" 20000 (readAll client)
+  await send
+  await serve
+  assertResponseCount res 3
+  let got := tokens (String.fromUTF8! res) "[" "]"
+  unless got == (List.range 500).map toString ++ ["tail", "tail"] do
+    throw <| IO.userError s!"chunks out of order: {got.take 10} ... {got.drop 495}"
+  expectNoFailures failures
+
+#eval runGroup "late reader gets the whole streamed body in order" <| Async.block do
+  let (client, conn) ← connectPair
+  let raw := mkGetClose "/big"
+  let failures ← IO.mkRef #[]
+  let pad := String.ofList (List.replicate 4000 'x')
+  let h := recordingHandler failures fun _ =>
+    Response.ok |>.stream fun s => do
+      for i in [0:1024] do
+        s.send (Chunk.ofByteArray s!"<{i}>{pad}".toUTF8)
+  let serve ← async (Server.serveConnection conn h config |>.run)
+  let send ← async do client.send raw.toUTF8; client.shutdown
+  sleep 300
+  let res ← withDeadline "late reader" 20000 (readAll client)
+  await send
+  await serve
+  let got := tokens (String.fromUTF8! res) "<" ">"
+  unless got == (List.range 1024).map toString do
+    throw <| IO.userError s!"chunks out of order or missing: {got.length} tokens"
+  expectNoFailures failures
+
+/-- Sends `raw` from a fresh client and drops the client, which closes it without reading. -/
+def sendFromDroppedClient (raw : ByteArray) : Async TCP.Socket.Client := do
+  let (client, conn) ← connectPair
+  client.send raw
+  return conn
+
+#eval runGroup "failed write closes the connection before the next pipelined handler" <| Async.block do
+  let conn ← sendFromDroppedClient (mkGet "/first" ++ mkPost "/second" "x" ++ mkGetClose "/third").toUTF8
+  let failures ← IO.mkRef #[]
+  let later ← IO.mkRef #[]
+  let big := ByteArray.mk (Array.replicate (8 * 1024 * 1024) 'x'.toUInt8)
+  let h := recordingHandler failures fun req => do
+    let uri := toString req.line.uri
+    if uri == "/first" then
+      -- Let the client's close reach the server before writing.
+      sleep 200
+      Response.ok |>.bytes big
+    else
+      later.modify (·.push uri)
+      Response.ok |>.text "later"
+  withDeadline "failed write" 20000 (Server.serveConnection conn h config |>.run)
+  let ran ← later.get
+  unless ran.isEmpty do
+    throw <| IO.userError s!"handlers ran after a failed write: {ran}"
+  if (← failures.get).isEmpty then
+    throw <| IO.userError "the failed write was not reported"
+
+#eval runGroup "stalled streamed body still times out" <| Async.block do
+  let (client, conn) ← connectPair
+  let failures ← IO.mkRef #[]
+  let h := recordingHandler failures fun _ =>
+    Response.ok |>.stream fun s => do
+      s.send (Chunk.ofByteArray "[first]".toUTF8)
+      sleep 5000
+      s.send (Chunk.ofByteArray "[late]".toUTF8)
+  let cfg := { config with lingeringTimeout := 300 }
+  let serve ← async (Server.serveConnection conn h cfg |>.run)
+  client.send (mkGet "/stall").toUTF8
+  let start ← IO.monoMsNow
+  withDeadline "stalled stream" 3000 (await serve)
+  let elapsed := (← IO.monoMsNow) - start
+  unless elapsed < 3000 do throw <| IO.userError s!"took {elapsed} ms"
+  let res ← withDeadline "stalled read" 3000 (readAll client)
+  assertContains res "[first]"
+  assertAbsent res "[late]"
+
+#eval runGroup "client that leaves mid-stream ends the connection" <| Async.block do
+  let failures ← IO.mkRef #[]
+  let h := recordingHandler failures fun _ =>
+    Response.ok |>.stream fun s => do
+      for i in [0:200] do
+        s.send (Chunk.ofByteArray s!"[{i}]".toUTF8)
+        sleep 20
+  let server ← TCP.Socket.Server.mk
+  server.bind (SocketAddressV4.mk (.ofParts 127 0 0 1) 0)
+  server.listen 128
+  let addr ← server.getSockName
+  let serve ← async do
+    let conn ← server.accept
+    Server.serveConnection conn h config |>.run
+  let drop : Async Unit := do
+    let client ← TCP.Socket.Client.mk
+    client.connect addr
+    client.send (mkGet "/slow").toUTF8
+    discard <| client.recv? 65536
+  drop
+  let start ← IO.monoMsNow
+  withDeadline "client left" 3000 (await serve)
+  let elapsed := (← IO.monoMsNow) - start
+  unless elapsed < 3000 do throw <| IO.userError s!"took {elapsed} ms"
+  if (← failures.get).isEmpty then
+    throw <| IO.userError "the lost client was not reported"
+
+/-- Reads one response whose body is `size` bytes long. -/
+def readResponse (client : TCP.Socket.Client) (size : Nat) : Async ByteArray := do
+  let mut out := ByteArray.empty
+  repeat
+    if out.size ≥ 512 then
+      let head := (String.fromUTF8! (out.extract 0 512)).splitOn "\x0d\n\x0d\n" |>.headD ""
+      if out.size ≥ head.utf8ByteSize + 4 + size then break
+    match ← client.recv? 65536 with
+    | none => break
+    | some c => out := out ++ c
+  return out
+
+#eval runGroup "keep-alive timeout starts once the response is written" <| Async.block do
+  let (client, conn) ← connectPair
+  let failures ← IO.mkRef #[]
+  let size := 8 * 1024 * 1024
+  let big := ByteArray.mk (Array.replicate size 'x'.toUInt8)
+  let h := recordingHandler failures fun req =>
+    if toString req.line.uri == "/big" then Response.ok |>.bytes big else Response.ok |>.text "[next]"
+  let cfg := { config with keepAliveTimeout := ⟨300, by decide⟩ }
+  let serve ← async (Server.serveConnection conn h cfg |>.run)
+  client.send (mkGet "/big").toUTF8
+  -- Reading the response starts after the keep-alive timeout.
+  sleep 1000
+  let first ← withDeadline "slow reader" 20000 (readResponse client size)
+  assertStatus first "HTTP/1.1 200"
+  client.send (mkGetClose "/next").toUTF8
+  let second ← withDeadline "second response" 5000 (readAll client)
+  await serve
+  assertContains second "[next]"
+  expectNoFailures failures
diff --git a/tests/async/elab/async_http_write_failure.lean b/tests/async/elab/async_http_write_failure.lean
new file mode 100644
index 0000000..8d448db
--- /dev/null
+++ b/tests/async/elab/async_http_write_failure.lean
@@ -0,0 +1,50 @@
+import Std.Http.Test.Helpers
+
+/-!
+A transport whose write reports its error after a delay, like TCP: the failed write must be reported
+once, the connection must close, and the handler of the pipelined `POST` behind it must not run.
+Also covers a write that fails when it starts.
+-/
+
+open Std.Async
+open Std Http Internal Test
+
+structure FailingTransport where
+  inner : Mock.Server
+  later : Bool
+  writes : IO.Ref Nat
+
+instance : Transport FailingTransport where
+  recv t expect := Transport.recv t.inner expect
+  sendAll t data := do
+    let n ← t.writes.modifyGet fun n => (n, n + 1)
+    if n == 0 then
+      if t.later then sleep 50
+      throw <| IO.userError "write failed"
+    Transport.sendAll t.inner data
+  recvSelector t expect := Transport.recvSelector t.inner expect
+  close t := Transport.close t.inner
+
+def runFailing (later : Bool) : IO Unit := Async.block do
+  let (client, server) ← Mock.new
+  let transport : FailingTransport := { inner := server, later, writes := ← IO.mkRef 0 }
+  let failures ← IO.mkRef (#[] : Array String)
+  let ran ← IO.mkRef (#[] : Array String)
+  let h : Server.StatelessHandler := {
+    onRequest := fun req => do
+      let uri := toString req.line.uri
+      unless uri == "/first" do ran.modify (·.push uri)
+      Response.ok |>.text uri
+    onFailure := fun e => failures.modify (·.push (toString e))
+  }
+  client.send (mkGet "/first" ++ mkPost "/second" "side effect" ++ mkGetClose "/third").toUTF8
+  Async.race
+    (Server.serveConnection transport h defaultConfig |>.run)
+    (do sleep 5000; throw <| IO.userError "the connection did not close")
+  unless (← ran.get).isEmpty do
+    throw <| IO.userError s!"handlers ran after a failed write: {← ran.get}"
+  unless (← failures.get) == #["write failed"] do
+    throw <| IO.userError s!"expected one reported failure, got {← failures.get}"
+
+#eval runGroup "write that fails later" (runFailing true)
+#eval runGroup "write that fails when started" (runFailing false)