Skip to content

Commit 030c280

Browse files
committed
[KP] Fix Headers timeout issue (#81140)
* temp fix request headers bug. issue 73849 * add a focused test * move test to FTR * adjust types * some minors * small adjustments
1 parent 2558031 commit 030c280

File tree

8 files changed

+123
-7
lines changed

8 files changed

+123
-7
lines changed

src/core/server/http/http_tools.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ interface ListenerOptions {
103103
export function createServer(serverOptions: ServerOptions, listenerOptions: ListenerOptions) {
104104
const server = new Server(serverOptions);
105105

106+
// remove fix + test as soon as update node.js to v12.19 https://github.com/elastic/kibana/pull/61587
107+
server.listener.headersTimeout =
108+
listenerOptions.keepaliveTimeout + 2 * server.listener.headersTimeout;
109+
106110
server.listener.keepAliveTimeout = listenerOptions.keepaliveTimeout;
107111
server.listener.setTimeout(listenerOptions.socketTimeout);
108112
server.listener.on('timeout', (socket) => {

src/core/server/http/integration_tests/request.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,6 @@ describe('KibanaRequest', () => {
313313
expect(resp3.body).toEqual({ requestId: 'gamma' });
314314
});
315315
});
316-
317316
describe('request uuid', () => {
318317
it('generates a UUID', async () => {
319318
const { server: innerServer, createRouter } = await server.setup(setupDeps);

src/core/server/http/test_utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ configService.atPath.mockReturnValue(
5050
allowFromAnyIp: true,
5151
ipAllowlist: [],
5252
},
53+
keepaliveTimeout: 120_000,
54+
socketTimeout: 120_000,
5355
} as any)
5456
);
5557

tasks/config/run.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ module.exports = function () {
175175
'--config',
176176
'test/server_integration/http/ssl_redirect/config.js',
177177
'--config',
178-
'test/server_integration/http/cache/config.js',
178+
'test/server_integration/http/platform/config.ts',
179179
'--config',
180180
'test/server_integration/http/ssl_with_p12/config.js',
181181
'--config',

test/server_integration/http/cache/index.js renamed to test/server_integration/http/platform/cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
19+
import { FtrProviderContext } from '../../services/types';
2020
// eslint-disable-next-line import/no-default-export
21-
export default function ({ getService }) {
21+
export default function ({ getService }: FtrProviderContext) {
2222
const supertest = getService('supertest');
2323

2424
describe('kibana server cache-control', () => {

test/server_integration/http/cache/config.js renamed to test/server_integration/http/platform/config.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
import { FtrConfigProviderContext } from '@kbn/test/types/ftr';
1920

20-
export default async function ({ readConfigFile }) {
21+
// eslint-disable-next-line import/no-default-export
22+
export default async function ({ readConfigFile }: FtrConfigProviderContext) {
2123
const httpConfig = await readConfigFile(require.resolve('../../config'));
2224

2325
return {
24-
testFiles: [require.resolve('./')],
26+
testFiles: [require.resolve('./cache'), require.resolve('./headers')],
2527
services: httpConfig.get('services'),
2628
servers: httpConfig.get('servers'),
2729
junit: {
28-
reportName: 'Http Cache-Control Integration Tests',
30+
reportName: 'Kibana Platform Http Integration Tests',
2931
},
3032
esTestCluster: httpConfig.get('esTestCluster'),
3133
kbnTestServer: httpConfig.get('kbnTestServer'),
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import Http from 'http';
20+
import Url from 'url';
21+
import { FtrProviderContext } from '../../services/types';
22+
23+
// @ts-ignore
24+
import getUrl from '../../../../src/test_utils/get_url';
25+
26+
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
27+
const oneSec = 1_000;
28+
29+
// eslint-disable-next-line import/no-default-export
30+
export default function ({ getService }: FtrProviderContext) {
31+
const config = getService('config');
32+
33+
describe('headers timeout ', () => {
34+
it('issue-73849', async () => {
35+
const agent = new Http.Agent({
36+
keepAlive: true,
37+
});
38+
const { protocol, hostname, port } = Url.parse(getUrl.baseUrl(config.get('servers.kibana')));
39+
40+
function performRequest() {
41+
return new Promise((resolve, reject) => {
42+
const req = Http.request(
43+
{
44+
protocol,
45+
hostname,
46+
port,
47+
path: '/',
48+
method: 'GET',
49+
agent,
50+
},
51+
function (res) {
52+
let data = '';
53+
res.on('data', (chunk) => {
54+
data += chunk;
55+
});
56+
res.on('end', () => resolve(data));
57+
}
58+
);
59+
60+
req.on('socket', (socket) => {
61+
socket.write('GET / HTTP/1.1\r\n');
62+
setTimeout(() => {
63+
socket.write('Host: localhost\r\n');
64+
}, oneSec);
65+
setTimeout(() => {
66+
// headersTimeout doesn't seem to fire if request headers are sent in one packet.
67+
socket.write('\r\n');
68+
req.end();
69+
}, 2 * oneSec);
70+
});
71+
72+
req.on('error', reject);
73+
});
74+
}
75+
76+
await performRequest();
77+
const defaultHeadersTimeout = 40 * oneSec;
78+
await delay(defaultHeadersTimeout + oneSec);
79+
await performRequest();
80+
});
81+
});
82+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { GenericFtrProviderContext } from '@kbn/test/types/ftr';
21+
import { services as kibanaCommonServices } from '../../common/services';
22+
import { services as kibanaApiIntegrationServices } from '../../api_integration/services';
23+
24+
export type FtrProviderContext = GenericFtrProviderContext<
25+
typeof kibanaCommonServices & { supertest: typeof kibanaApiIntegrationServices.supertest },
26+
{}
27+
>;

0 commit comments

Comments
 (0)