Skip to content

fix(@angular/build): safeguard Karma builder stream controller against closed state - #33660

Merged
alan-agius4 merged 1 commit into
angular:mainfrom
clydin:karma-close-check
Jul 28, 2026
Merged

fix(@angular/build): safeguard Karma builder stream controller against closed state#33660
alan-agius4 merged 1 commit into
angular:mainfrom
clydin:karma-close-check

Conversation

@clydin

@clydin clydin commented Jul 27, 2026

Copy link
Copy Markdown
Member

When running Karma tests or when stream consumers (such as Architect test harness executeOnce) cancel the builder output stream early, both ProgressNotifierReporter.onRunComplete and karma.Server exit callbacks can attempt to enqueue results or close the ReadableStreamController.

Under WHATWG Streams specification rules, calling .enqueue() or .close() on a controller whose desiredSize is null (closed or cancelled) throws TypeError [ERR_INVALID_STATE]: Invalid state: Controller is already closed.

This change checks controller.desiredSize !== null and wraps enqueue/close calls in a try-catch block to gracefully handle closed controllers.

@clydin clydin added the target: patch This PR is targeted for the next patch release label Jul 27, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces safeguards when calling controller.enqueue and controller.close on stream controllers in Karma builders to prevent errors if the stream is already closed or cancelled. The reviewer identified additional asynchronous paths in both application_builder.ts and progress-reporter.ts where similar safeguards are missing and should be applied to prevent potential TypeError or unhandled promise rejections.

Comment thread packages/angular/build/src/builders/karma/application_builder.ts
Comment thread packages/angular/build/src/builders/karma/progress-reporter.ts
…t closed state

When running Karma tests or when stream consumers (such as Architect test harness `executeOnce`) cancel the builder output stream early, both `ProgressNotifierReporter.onRunComplete` and `karma.Server` exit callbacks can attempt to enqueue results or close the `ReadableStreamController`.

Under WHATWG Streams specification rules, calling `.enqueue()` or `.close()` on a controller whose `desiredSize` is `null` (closed or cancelled) throws `TypeError [ERR_INVALID_STATE]: Invalid state: Controller is already closed`.

This change checks `controller.desiredSize !== null` and wraps enqueue/close calls in a try-catch block to gracefully handle closed controllers.
@clydin
clydin force-pushed the karma-close-check branch from 65c2ad4 to f58c781 Compare July 27, 2026 15:15
@clydin clydin added the action: review The PR is still awaiting reviews from at least one requested reviewer label Jul 27, 2026
@clydin
clydin requested a review from alan-agius4 July 27, 2026 15:38
@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jul 28, 2026
@alan-agius4
alan-agius4 merged commit 5914877 into angular:main Jul 28, 2026
40 of 41 checks passed
@alan-agius4

Copy link
Copy Markdown
Collaborator

This PR was merged into the repository. The changes were merged into the following branches:

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

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants