Skip to content

rxResource abort callback throws when stream function threw before returning an Observable #63341

@wartab

Description

@wartab

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

res = rxResource({
    params: () => 'hello',
    stream: () => {
      throw 'some error';
    },
  });

If for some reason, the stream function throws before returning an Observable, on cleanup, you get an error message in the console.

The reason for this is here:

let sub: Subscription;
// Track the abort listener so it can be removed if the Observable completes (as a memory
// optimization).
const onAbort = () => sub.unsubscribe();
params.abortSignal.addEventListener('abort', onAbort);

It is wrongly assumed here that sub can't be undefined. But in this specific case, it can be.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/2ckt7egc-f14axvuk?file=src%2Fmain.ts

Please provide the exception or error you saw

.webcontainer@runtime.96435430.js:26 ERROR TypeError: Cannot read properties of undefined (reading 'unsubscribe')
    at AbortSignal.onAbort (rxjs-interop.mjs:313:39)
    at _ZoneDelegate.invokeTask (zone.js:431:33)
    at debug_node.mjs:16349:55
    at AsyncStackTaggingZoneSpec.onInvokeTask (debug_node.mjs:16349:36)
    at _ZoneDelegate.invokeTask (zone.js:430:38)
    at Object.onInvokeTask (debug_node.mjs:16672:33)
    at _ZoneDelegate.invokeTask (zone.js:430:38)
    at ZoneImpl.runTask (zone.js:161:47)
    at ZoneTask.invokeTask [as invoke] (zone.js:515:34)
    at invokeTask (zone.js:1141:18)

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 20.1.3
Node: 22.14.0
Package Manager: npm 10.8.2
OS: win32 x64

Angular: 20.1.3
... animations, build, cdk, cli, common, compiler, compiler-cli
... core, forms, language-service, localize, material
... platform-browser, platform-browser-dynamic, router

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.2001.3
@angular-devkit/core         20.1.3
@angular-devkit/schematics   20.1.3
@schematics/angular          20.1.3
rxjs                         7.8.2
typescript                   5.8.3
zone.js                      0.15.1

Anything else?

I have a PR ready for the fix, but writing the test is much harder than it might look, because the abortSignal hooks don't seem to be easily accessible (unless there's a hack I don't know about) and are called asynchronously, it seems.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtime

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions