Skip to content

Image-result download leaves the response stream open after HTTP errors #984

Closed

Description

What happened?

When an image-result URL returns an HTTP error, materializeImage throws without cancelling its streaming response body. The download can keep receiving data after the tool has reported failure. The unsupported-content-type branch has the same missing cleanup by inspection.

Expected: cancel the body before returning an HTTP/MIME failure.

Steps to reproduce

From tag v2.0.151, run this isolated test. It uses the unchanged transport and materializeImage functions. Image-URL extraction is stubbed to select the URL branch and the loopback fixture is passed directly to the transport; no model call or real image generation is made.

Standalone reproduction command
python3 - <<'PY' | bun run -
from pathlib import Path
r=Path('backend/cli/src')
s=(r/'settings/network.ts').read_text()
g=(r/'tool/generate-image.ts').read_text()
print('import {isIP} from "node:net"; import {request as httpRequest,createServer} from "node:http"; import {request as httpsRequest} from "node:https"; import {Readable} from "node:stream";')
print(s[s.index('  async function pinnedFetch('):s.index('  /** Policy-aware fetch.')])
print('const Network={fetch:async(raw,init)=>pinnedFetch(new URL(raw),init,"127.0.0.1",undefined,true)}; const MAX_IMAGE_BYTES=1000; const extractGeneratedImage=()=>{throw Error("without returning image bytes")}; const extractGeneratedImageURL=v=>v.url;')
print(g[g.index('async function materializeImage('):g.index('export function generatedImageAttachments(')])
print('''
let writes=0,closed=false;
const server=createServer((req,res)=>{
  res.writeHead(404,{"content-type":"text/plain"});res.write("ERROR");
  const timer=setInterval(()=>{writes++;res.write("MORE");},20);
  res.on("close",()=>{closed=true;clearInterval(timer);});
});
await new Promise(r=>server.listen(0,"127.0.0.1",r));
const controller=new AbortController();
try {
  try {await materializeImage({url:"http://127.0.0.1:"+server.address().port},controller.signal,async()=>{});}
  catch(e){console.log(e.message);}
  await Bun.sleep(100);
  console.log({socketClosed:closed,additionalChunks:writes});
} finally {
  controller.abort();server.closeAllConnections();server.close();
  process.exit(0); // standalone fixture; avoid keeping the 120-second timer alive
}
''')
PY

Logs and screenshots

The generated image could not be downloaded (404).
socketClosed=false additionalChunks=4

Code location

generate-image.ts:256–259. Both branches throw after receiving a streaming Response. In contrast, webfetch cancels its body on non-success status.

This is a request-lifetime/resource-cleanup finding, not a claim of permanent connection leakage; the request's two-minute timeout can eventually terminate it.

How did you install OpenScience?

Official desktop ZIP and npm; isolated reproduction uses tag v2.0.151 source.

OpenScience version

2.0.151 (latest npm at reporting time).

Provider and model

Not applicable; no model calls or credits used.

Operating system

macOS 27.0 ARM64; Bun 1.3.13. Not yet tested inside the packaged desktop runtime.

Action approval mode

Not applicable to the isolated fixture.

Activity

  1. aayambansal commented on Oct 18, 2026

    @aayambansal
    Member

    Fixed in #993, now squash merged to main. HTTP-error and unsupported-format image downloads cancel their response bodies before returning an error; a cancellation failure preserves the original download error.

    The five regressions use the real tool and a local HTTP stream bridge, cover both error paths and cancellation failures, and verify successful image bytes remain unchanged. Required CI and runtime checks passed. Existing installed releases need an update containing this commit.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions