github/copilot-sdk · warning

Failed to flush final factory progress after the factory…

Error message

Failed to flush final factory progress after the factory body settled

What it means

CopilotSession buffers periodic factory progress lines and flushes any remaining ones once the factory body settles during close(). This warning is logged when the final send() of those buffered lines rejects. The session still closes; only the last progress update is lost.

Solutions

  1. Verify the underlying connection/transport is still alive before calling close(); retry close() after reconnecting if the progress matters.
  2. Check logs for an earlier transport error - the flush failure is usually a symptom, not the root cause.
  3. If the loss of final progress is acceptable, treat this warning as informational and proceed.
  4. Ensure you do not call send() or close() concurrently from multiple code paths.

Example fix

// before
await session.close();
// after
try {
  await session.close();
} catch (err) {
  console.error('close failed', err);
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!session.isConnected?.()) {
  console.warn('transport down; final progress flush will likely fail');
}

Try / catch

try {
  await session.close();
} catch (error) {
  console.error('Session close failed; final progress may be lost', error);
  // decide whether to reconnect and re-flush or abandon
}

Prevention

When it happens

Trigger: Calling close() on a session whose factory body emitted progress lines, while the transport/underlying connection has already been terminated or is failing, so this.send(lines) rejects during the final flush.

Common situations: Network drop or server shutdown right as close() is called; calling close() twice so the stream is already closed; killing the parent process/socket before close completes.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/7b7fd7f7aa6a8541. Report an issue: GitHub.

Appendix: source

Thrown at nodejs/src/session.ts:296

        }
    }

    async close(): Promise<void> {
        this.closed = true;
        this.clearFlushTimer();
        const lines = this.pending.splice(0);
        await this.flushTail;
        if (this.flushFailed) {
            console.warn(
                "Ignoring a background factory progress flush failure after the factory body settled",
                this.flushError
            );
        }
        if (lines.length > 0) {
            try {
                await this.send(lines);
            } catch (error) {
                console.warn(
                    "Failed to flush final factory progress after the factory body settled",
                    error
                );
            }
        }
    }

    private scheduleFlush(): void {
        if (this.flushTimer !== undefined) {
            return;
        }
        this.flushTimer = setTimeout(() => {
            this.flushTimer = undefined;
            void this.flush().catch(() => {});
        }, FACTORY_LOG_FLUSH_DELAY_MS);
        this.flushTimer.unref?.();
    }

View on GitHub (pinned to cd8cf15dc3)