Skip to content

Commit

Permalink
fix(Subscriber): don't leak destination (#6116)
Browse files Browse the repository at this point in the history
* test: rearrange shareReplay test slightly

* test: add failing Subscriber destination leak test

* fix: null destination on unsubscribe

* fix: capture destination before onNext, etc.
  • Loading branch information
cartant authored Mar 15, 2021
1 parent f3b2f05 commit 5bba36c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
28 changes: 27 additions & 1 deletion spec/Subscriber-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import { SafeSubscriber } from 'rxjs/internal/Subscriber';
import { Subscriber, Observable, config, of } from 'rxjs';
import { Subscriber, Observable, config, of, Observer } from 'rxjs';
import { asInteropSubscriber } from './helpers/interop-helper';
import { getRegisteredTeardowns } from './helpers/subscription';

Expand Down Expand Up @@ -242,4 +242,30 @@ describe('Subscriber', () => {
expect(consumer.valuesProcessed).not.to.equal(['new', 'new']);
});
});

const FinalizationRegistry = (global as any).FinalizationRegistry;
if (FinalizationRegistry) {

it('should not leak the destination', (done) => {
let observer: Observer<number> | undefined = {
next() { /* noop */ },
error() { /* noop */ },
complete() { /* noop */ }
};

const registry = new FinalizationRegistry((value: any) => {
expect(value).to.equal('observer');
done();
});
registry.register(observer, 'observer');

const subscription = of(42).subscribe(observer);

observer = undefined;
global.gc();
});

} else {
console.warn(`No support for FinalizationRegistry in Node ${process.version}`);
}
});
3 changes: 2 additions & 1 deletion spec/operators/shareReplay-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,12 @@ describe('shareReplay operator', () => {
if (FinalizationRegistry) {

it('should not leak the subscriber for sync sources', (done) => {
let callback: (() => void) | undefined = () => { /* noop */ };

const registry = new FinalizationRegistry((value: any) => {
expect(value).to.equal('callback');
done();
});
let callback: (() => void) | undefined = () => { /* noop */ };
registry.register(callback, 'callback');

const shared = of(42).pipe(shareReplay(1));
Expand Down
1 change: 1 addition & 0 deletions src/internal/Subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class Subscriber<T> extends Subscription implements Observer<T> {
if (!this.closed) {
this.isStopped = true;
super.unsubscribe();
this.destination = null!;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/internal/operators/OperatorSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class OperatorSubscriber<T> extends Subscriber<T> {
try {
onNext(value);
} catch (err) {
this.destination.error(err);
destination.error(err);
}
}
: super._next;
Expand All @@ -52,7 +52,7 @@ export class OperatorSubscriber<T> extends Subscriber<T> {
onError(err);
} catch (err) {
// Send any errors that occur down stream.
this.destination.error(err);
destination.error(err);
} finally {
// Ensure teardown.
this.unsubscribe();
Expand All @@ -65,7 +65,7 @@ export class OperatorSubscriber<T> extends Subscriber<T> {
onComplete();
} catch (err) {
// Send any errors that occur down stream.
this.destination.error(err);
destination.error(err);
} finally {
// Ensure teardown.
this.unsubscribe();
Expand Down

0 comments on commit 5bba36c

Please sign in to comment.