Skip to content

Close adapter-node server gracefully on SIGINT and SIGTERM #9540

@vekunz

Description

@vekunz

Describe the problem

Currently, when building a SvelteKit app with the adapter-node, the production build does not handle SIGINT or SIGTERM signals. The result of this is that the SvelteKit app cannot be gracefully stopped, e.g. in a dockerized environment (Kubernetes in our case). The environment has to kill the app in order to shut it down. In a continuous deployment scenario this results in many broken HTTP requests.

Describe the proposed solution

Since this is a problem for anyone using the node adapter, this should be "fixed" generally in adapter-node.

The index.js file of adapter-node can be extended with this snippet (and the same for SIGTERM also):

process.on('SIGINT', () => {
	console.log('Got SIGINT. Starting graceful shutdown.');
	server.server?.close(() => {
		console.log('Server closed');
		process.exit(0);
	});
	if (
		// This function only exists since Node 18
		server.server !== undefined &&
		'closeIdleConnections' in server.server &&
		typeof server.server.closeIdleConnections === 'function'
	) {
		server.server.closeIdleConnections();
	}
});

Alternatives considered

Alternatively, anyone has to build the same custom logic for this.

Importance

would make my life easier

Additional Information

If the SvelteKit team says that such logic should be implemented in adapter-node, I can create a Pull Request for this (actually I already wrote the code for this in a fork and just need to create the Pull Request for it, but I wanted to ask first).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions