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

Add coverage for duplicate file closes #158

Merged
merged 2 commits into from
Aug 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion test/file.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const ChildProcess = require('child_process');
const Events = require('events');
const Fs = require('fs');
const Os = require('os');
const Path = require('path');
Expand Down Expand Up @@ -741,7 +742,9 @@ describe('file', () => {

it('closes file handlers when not using a manually open file stream', { skip: process.platform === 'win32' }, async () => {

const server = await provisionServer();
// This test doesn't rely on inert but is a fair test of hapi's handling of (file) streams
const server = Hapi.server();

server.route({
method: 'GET',
path: '/file',
Expand All @@ -755,6 +758,14 @@ describe('file', () => {
const res2 = await server.inject({ url: '/file', headers: { 'if-none-match': res1.headers.etag } });
expect(res2.statusCode).to.equal(304);

const file1 = res1.request.response.source;
const file2 = res2.request.response.source;

await Promise.all([
file1.closed || Events.once(file1, 'close'),
file2.closed || Events.once(file2, 'close')
]);

await new Promise((resolve) => {

const cmd = ChildProcess.spawn('lsof', ['-p', process.pid]);
Expand Down Expand Up @@ -1461,6 +1472,36 @@ describe('file', () => {
});
});

it('permits duplicate closes', async () => {

const server = await provisionServer();

server.route({
method: 'GET',
path: '/file',
handler: { file: Path.join(__dirname, '..', 'package.json') },
options: {
ext: {
onPostHandler: {
method: ({ response }) => {

// In practice the framework does not call close() twice,
// but inert assumes it may happen, so we simulate it here
response._processors.close(response);

// Reassigning a new response causes a second close() by hapi
return { reassigned: true };
}
}
}
}
});

const res = await server.inject('/file');

expect(res.result).to.equal({ reassigned: true });
});

it('has not leaked file descriptors', { skip: process.platform === 'win32' }, async () => {

// validate that all descriptors has been closed
Expand Down