Repository navigation
Description
Environment details
@google-cloud/bigquery-storageversion: 6.3.0- Node.js version: v24.18.1
- OS: macOS
What happens
WriterClient.createStreamConnection pushes every StreamConnection onto this._connections.connectionList, and only WriterClient.close() empties that list. A long-lived WriterClient that opens and closes stream connections one at a time therefore keeps every closed connection reachable, with its listeners and its pending-write array, until the process exits.
For a service that keeps one WriterClient for its lifetime and recycles streams (idle timeouts, max age, connection errors), memory grows without bound.
Steps to reproduce
The script opens and closes connections against a dead local endpoint, so it sends nothing anywhere. Each connection gets an error listener that closes over a 256 KB object, so retention shows in memory.
// Usage: node --expose-gc retain.js <pkg-dir> <n> <fix: none|splice>
const { createRequire } = require("module");
const path = require("path");
const [pkgDir, nArg, fix] = process.argv.slice(2);
const req = createRequire(path.join(pkgDir, "package.json"));
const { managedwriter } = req("@google-cloud/bigquery-storage");
const { grpc } = req("google-gax");
const N = Number(nArg);
const ENTRY_BYTES = 256 * 1024;
async function main() {
const client = new managedwriter.WriterClient({
projectId: "local",
servicePath: "127.0.0.1",
port: 1,
sslCreds: grpc.credentials.createInsecure(),
});
const refs = [];
let pendingKept = 0;
global.gc();
const before = process.memoryUsage().heapUsed + process.memoryUsage().arrayBuffers;
for (let i = 0; i < N; i++) {
const connection = await client.createStreamConnection({
streamId: `projects/local/datasets/d/tables/t${i}/streams/_default`,
});
const entry = { payload: Buffer.alloc(ENTRY_BYTES, 1) };
connection.onConnectionError(() => void entry);
refs.push(new WeakRef(entry));
const pw = connection.write({ protoRows: { rows: { serializedRows: [Buffer.alloc(1024)] } } });
pw.getResult().catch(() => {});
for (const p of connection.getPendingWrites()) p.abort();
connection.close();
pendingKept += connection.getPendingWrites().length;
if (fix === "splice") {
const list = client._connections.connectionList;
list.splice(list.indexOf(connection), 1);
}
}
for (let k = 0; k < 10; k++) { await new Promise((r) => setTimeout(r, 200)); global.gc(); }
const after = process.memoryUsage().heapUsed + process.memoryUsage().arrayBuffers;
console.log(JSON.stringify({
fix,
opened_and_closed: N,
connectionList_length: client._connections.connectionList.length,
entries_still_alive: refs.filter((r) => r.deref() !== undefined).length,
aborted_pending_writes_kept: pendingKept,
retained_mb: Math.round((after - before) / 1048576),
}));
client.close();
}
main().catch((e) => { console.error(e); process.exit(1); });Output
$ node --expose-gc retain.js "$PWD" 2000 none
{"fix":"none","opened_and_closed":2000,"connectionList_length":2000,"entries_still_alive":2000,"aborted_pending_writes_kept":2000,"retained_mb":505}
$ node --expose-gc retain.js "$PWD" 2000 splice
{"fix":"splice","opened_and_closed":2000,"connectionList_length":0,"entries_still_alive":0,"aborted_pending_writes_kept":2000,"retained_mb":-1}Without splice, all 2,000 closed connections stay in connectionList, and everything their listeners close over stays alive. Removing each closed connection from the list frees it all.
Expected
A StreamConnection leaves connectionList when it is closed, so a long-lived WriterClient holds only its open connections.
Where in the code
handwritten/bigquery-storage/src/managedwriter/writer_client.ts:299:this._connections.connectionList.push(streamConnection);handwritten/bigquery-storage/src/managedwriter/writer_client.ts:336-344:close(), the only place the list is emptied.
One possible fix: have StreamConnection.close() (or its close event) remove the connection from its WriterClient's list.
Activity