Open Bug 2092755 Opened 16 days ago Updated 14 days ago

Uploaded file stays open in the parent process after a service-worker-intercepted upload (blocks deleting the file / ejecting the USB drive)

Categories

(Core :: Networking, defect, P2)

defect

Tracking

()

ASSIGNED

People

(Reporter: cristian.vat, Assigned: cristian.vat)

References

Details

(Whiteboard: [necko-triaged][necko-priority-next])

Attachments

(1 file, 1 external file)

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/157.0.0.0 Safari/537.36

Steps to reproduce

Environment: Windows 11 (build 26300) x64, fresh profiles. Reproduces on Nightly 162.0a1 (BuildID 20261113085116, mozilla-central 02edb0c1927d7c3c87c8ed76e232f616fa76ac07) and on Release 160.0.

  1. Unzip the attached testcase, run node server.js, open http://localhost:8000/ and reload once until the page says "controlling this page". The service worker only has a fetch handler that does not call respondWith().
  2. Pick any local file. The page uploads file.slice(0, file.size) with fetch() and prints "upload finished".
  3. Try to delete the file in Explorer, or eject the USB drive it is on.

Real-world trigger: attaching a file in Gmail (whose service worker intercepts the upload), sending the email, then trying to eject the USB stick the file came from. Windows' Kernel-PnP event 225 names firefox.exe (parent process) as the process blocking the eject.

Actual results

Windows reports the file is in use by Firefox. The parent firefox.exe keeps a read handle to the file for minutes after the upload finished (6–21 minutes with Gmail), until the redirected-away HTTP channels are garbage-collected.

With the service worker, 5 of 6 upload shapes leave the file open: fetch/XHR with a File or file.slice() body, XHR FormData, and chunked slices. Without the service worker (tick "Control" in the testcase) none do. The uploaded bytes are correct in all cases.

Analysis

  • Blob/File streams (FileBlobImpl) are nsFileInputStreams with DEFER_OPEN | CLOSE_ON_EOF | REOPEN_ON_REWIND | SHARE_DELETE, wrapped in SlicedInputStream for slices.
  • HttpBaseChannel::InternalSetUploadStream sizes the upload via InputStreamLengthHelper::GetAsyncLength() → nsFileStreamBase::Available(), which calls DoPendingOpen() and opens the file just to report its size (see bug 711883).
  • On each redirect, HttpBaseChannel::SetupReplacementChannel rewinds the upload stream (Seek(NS_SEEK_SET, 0); a slice seeks to its start) → nsFileInputStream::SeekInternal calls DoPendingOpen() → opens the file.
  • Service worker interception is implemented as internal redirects (nsHttpChannel → InterceptedHttpChannel → nsHttpChannel). The bytes on the wire come from a clone, which is read to EOF and closed. The original stream, opened by the steps above but never read, is kept alive by the redirected-away channels until they are GC'd. Without a service worker the same channel reads the original to EOF and CLOSE_ON_EOF closes it.
  • Confirmed from a memory dump of the affected parent process: handle → PRFileDesc (open) → nsFileInputStream (eOpened, flags 0x3c, position 0) → SlicedInputStream (start 0, length = file size) → mUploadStream of an InterceptedHttpChannel and an nsHttpChannel, both with status NS_BINDING_REDIRECTED.
  • An instrumented build printing a stack on every open shows exactly two openers: AvailableEvent::Run → nsFileStreamBase::Available and nsFileInputStream::SeekInternal.

Patch (will submit to Phabricator)

Keep deferred file input streams deferred for length queries and seeks:

  • nsFileInputStream::Available() in the deferred state answers from the file size (fresh stat) minus the pending position, without opening.
  • nsFileInputStream::SeekInternal() in the deferred state records the target position; nsFileStreamBase::DoOpen() applies it right after the real open, and Tell() reports it.
  • On Linux, the deferred Available() keeps the existing NO_MAGIC_FILES behaviour (same check as DoOpen(), shared via a small helper, evaluated with statfs on the path).
  • New xpcshell tests in netwerk/test/unit/test_filestreams.js (without the patch they fail with NS_ERROR_FILE_IS_LOCKED on Windows), plus a Linux-only test for the NO_MAGIC_FILES behaviour.

Tested on top of main (09b8652111):

  • Linux (Ubuntu 26.04, opt): new and existing xpcshell tests (test_filestreams, test_NetUtil, test_file_protocol, test_post, test_reopen); headless mochitests dom/serviceworkers/test/test_file_upload.html, test_file_blob_upload.html, test_file_blob_response.html, dom/file/tests, dom/fetch/tests, dom/xhr/tests/test_xhr_send.html. 0 unexpected.
  • Windows 11 (opt, packaged): same xpcshell tests; service-worker upload harness: no file handle retained in any upload pattern, with or without a service worker; all uploads byte-exact (whole files and chunked slices at non-zero offsets).
  • Earlier, on Release 160 sources (Windows): the same mochitests, 0 unexpected.

AI-use note

I used an AI coding assistant (Claude) for the investigation (dump analysis, instrumentation) and the first draft of the patch and tests. I reviewed, built and tested everything myself and can explain every change.

Expected results

The file is released as soon as the upload finishes, as it is without a service worker, so the file can be deleted and the drive ejected right away.

Upload streams for File/Blob bodies are nsFileInputStreams with DEFER_OPEN.
Sizing the upload (Available()) and rewinding it on redirects
(Seek(NS_SEEK_SET, ...)) both forced the file open. Service worker
interception redirects the channel internally and sends the bytes from a
clone, so the original stream stayed open, unread, until the redirected-away
channels were garbage-collected, keeping the file locked for minutes.

While the open is still deferred, Available() now answers from a fresh stat
of the file minus the pending position, and SeekInternal() records the
target position, which DoOpen() applies after the real open and Tell()
reports. On Linux the deferred Available() keeps the NO_MAGIC_FILES
behaviour by applying the same check as DoOpen(), via a shared helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Assignee: nobody → cristian.vat
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
See Also: → 711883
Severity: -- → S3
Priority: -- → P2
Whiteboard: [necko-triaged][necko-priority-next]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: