Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node hangs upon exit when using @Sse and app.enableShutdownHooks() #6643

Closed
PeS82 opened this issue Mar 12, 2021 · 6 comments
Closed

Node hangs upon exit when using @Sse and app.enableShutdownHooks() #6643

PeS82 opened this issue Mar 12, 2021 · 6 comments

Comments

@PeS82
Copy link

PeS82 commented Mar 12, 2021

Bug Report

Current behavior

When annotating route with @Sse('tada') and using app.enableShutdownHooks(); and there are clients connected to the SSE endpoint the process does not exit when SIGINT (or possibly other) request to terminate is received.

One must press Ctrl+C twice to terminate. Or click Stop button in Webstorm twice.

When there are no clients connected to the endpoint the process is terminated correctly.

Input Code

import {NestFactory} from "@nestjs/core";
import {BeforeApplicationShutdown, Controller, Get, Injectable, MessageEvent, Module, Sse} from "@nestjs/common";
import {Subject} from "rxjs";
import {distinctUntilChanged, share} from "rxjs/operators";

@Injectable()
export class TestSseService implements BeforeApplicationShutdown {
    private events = new Subject<MessageEvent>();
    private c = 0;
    private readonly interval: NodeJS.Timeout;

    constructor() {
        this.interval = setInterval(() => {
            this.events.next({
                retry: 3,
                type: 'message',
                id: `id-${++this.c}`,
                data: {message: `number ${this.c}`}
            })
        }, 1000);
    }

    get events$() {
        return this.events.pipe(distinctUntilChanged(), share());
    }

    beforeApplicationShutdown(signal?: string): any {
        clearInterval(this.interval);
        console.log('App shutdown');
    }

}

@Controller('/api/test-sse')
export class TestSseController {
    constructor(private sseService: TestSseService) {
    }

    @Sse('sse')
    serverSideEvents() {
        return this.sseService.events$;
    }

    @Get('hello')
    getHello() {
        return 'howdy';
    }
}

@Module({
    controllers: [TestSseController],
    providers: [TestSseService]
})
export class TestSseModule {
}


async function bootstrap() {
    const app = await NestFactory.create(TestSseModule);
    app.enableShutdownHooks();
    await app.listen(3000);
}

bootstrap();

Expected behavior

App terminates when asked to.

Possible Solution

IDK

Environment


Nest version: 7.5.6
 
For Tooling issues:
- Node version: 12.3.1
- Platform: Windows

Oh, and thx for Nest.

@PeS82 PeS82 added the needs triage This issue has not been looked into label Mar 12, 2021
@kamilmysliwiec
Copy link
Member

Please provide a minimum reproduction repository.

@kamilmysliwiec kamilmysliwiec added needs clarification and removed needs triage This issue has not been looked into labels Mar 12, 2021
@PeS82
Copy link
Author

PeS82 commented Mar 12, 2021

The code provided is not enough? It is the whole "project". If you do copy/paste to a file and run it, it shows the problem.

@jmcdo29
Copy link
Member

jmcdo29 commented Mar 12, 2021

When providing code for a reproduction, especially for a framework, a git repo is usually appreciated

@PeS82
Copy link
Author

PeS82 commented Mar 12, 2021

OK, I really do not get it but this code was honestly hanging in the morning. The same one does not now. Closing.

Sorry to bother you.

@PeS82 PeS82 closed this as completed Mar 12, 2021
@PeS82
Copy link
Author

PeS82 commented Mar 12, 2021

And... we're back. The difference between morning and now is that in the morning there were clients connected to the SSE endpoint.

I will edit the description and prepare repo.

@PeS82 PeS82 reopened this Mar 12, 2021
@PeS82
Copy link
Author

PeS82 commented Mar 12, 2021

Sigh. While preparing the repo I wrote simple client in vanilla javascript and in that one it works as expected as opposed to when our Angular client is connected.

Therefore, closing again 😃

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

No branches or pull requests

3 participants