Skip to content

Storage Emulator: PUT /upload/storage/v1/b/:bucketId/o ignores Content-Range and prematurely finalizes multi-chunk resumable uploads with HTTP 200 (deadlocks @google-cloud/storage) #11583

Todo

Description

@MarcelOldenkamp

[REQUIRED] Environment info

firebase-tools: 15.34.1

@google-cloud/storage: 7.22.0

Node.js: v24.22.1

Platform: macOS

[REQUIRED] Test case

import { Storage } from '@google-cloud/storage';
import { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';

const storage = new Storage({
  projectId: 'demo-test-project',
  apiEndpoint: 'http://127.0.0.1:9199', // or STORAGE_EMULATOR_HOST
});

const file = storage.bucket('demo-test-project.appspot.com').file('large.bin');
const writeStream = file.createWriteStream({
  resumable: true,
  chunkSize: 256 * 1024,
});

// Stream > chunkSize (e.g. 768 KiB)
const payload = Buffer.alloc(3 * 256 * 1024, 0x61);
await pipeline(Readable.from([payload]), writeStream);

[REQUIRED] Steps to reproduce

  1. Start the Firebase Storage Emulator (firebase emulators:start --only storage).
  2. Stream an object larger than chunkSize using @google-cloud/storage file.createWriteStream({ resumable: true, chunkSize: 256 * 1024 }) (the GCS SDK splits the upload into multiple PUT requests with Content-Range: bytes <start>-<end>/*).
  3. Wait for pipeline(...) to complete.

[REQUIRED] Expected behavior

Per the Google Cloud Storage resumable upload protocol, when receiving an incomplete chunk (Content-Range: bytes 0-262143/* or bytes 0-262143/786432 before the final byte):

  • The emulator appends the chunk bytes and responds with HTTP 308 Resume Incomplete (with a Range header acknowledging received bytes) while keeping the upload session ACTIVE.
  • Only once the final chunk completes the upload should finalizeResumableUpload run and return HTTP 200 OK.

[REQUIRED] Actual behavior

In src/emulator/storage/apis/gcloud.ts, the PUT /upload/storage/v1/b/:bucketId/o handler does not inspect Content-Range and unconditionally calls finalizeResumableUpload(uploadId) on the very first PUT:

uploadService.continueResumableUpload(uploadId, await reqBodyToBuffer(req));
upload = uploadService.finalizeResumableUpload(uploadId);

Because the first chunk receives HTTP 200 OK instead of 308 Resume Incomplete:

  1. @google-cloud/storage's Upload stream assumes the entire upload finished after the first chunk and emits uploadFinished without draining the rest of the Node.js Writable stream.
  2. The producer (pipeline / stream.write) waits for backpressure drain or stream completion that never arrives, causing the upload promise to hang indefinitely with no error.

Note: PUT in gcloud.ts also uses bodyParser.raw({ type: "*/*", limit: "130mb" }), which currently caps single-request uploads at 130 MiB when chunkSize is omitted as a workaround; supporting multi-chunk Content-Range uploads with HTTP 308 would also allow streaming uploads larger than 130 MiB (as long as each individual chunk is ≤ 130 MiB).

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions