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

Fix request aborted observable #73874

Closed
Closed
Show file tree
Hide file tree
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
50 changes: 50 additions & 0 deletions src/core/server/http/integration_tests/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { HttpService } from '../http_service';
import { contextServiceMock } from '../../context/context_service.mock';
import { loggingSystemMock } from '../../logging/logging_system.mock';
import { createHttpServer } from '../test_utils';
import { schema } from '@kbn/config-schema';

let server: HttpService;

Expand Down Expand Up @@ -173,6 +174,55 @@ describe('KibanaRequest', () => {
expect(completeSpy).toHaveBeenCalledTimes(1);
});

it('if in a preAuthHandler completes after the response is send', async () => {
const { server: innerServer, createRouter, registerOnPreAuth } = await server.setup(
setupDeps
);
const router = createRouter('/');

const nextSpy = jest.fn();
const completeSpy = jest.fn();
registerOnPreAuth((req, response, toolkit) => {
req.events.aborted$.subscribe({
next: nextSpy,
complete: completeSpy,
});

return toolkit.next();
});

let error: Error | undefined;
router.post(
{ path: '/', validate: { body: schema.any() } },
async (context, request, res) => {
// Spy should not have been called here
try {
expect(completeSpy).toHaveBeenCalledTimes(0);
} catch (err) {
error = err;
}

return res.ok({ body: 'ok' });
}
);

await server.start();

await supertest(innerServer.listener)
.post('/')
.send({
data: 'test',
})
.expect(200);

expect(nextSpy).toHaveBeenCalledTimes(0);
expect(completeSpy).toHaveBeenCalledTimes(1);

if (error) {
throw error;
}
});

it('completes & does not emit when request rejected', async () => {
const { server: innerServer, createRouter } = await server.setup(setupDeps);
const router = createRouter('/');
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/http/router/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class KibanaRequest<

private getEvents(request: Request): KibanaRequestEvents {
const finish$ = merge(
fromEvent(request.raw.req, 'end'), // all data consumed
Copy link
Contributor

@joshdover joshdover Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing, this change does appear to be correct. Right now the aborted$ observable will complete prior to the response being sent back, which may cause us to lose aborted events. This appears to only happen on POST requests that define body validation because the end event is being fired when the request body is read. On GET requests, the aborted$ Observable does not complete until the connection is closed.

I propose we bundle this fix with the addition of a real completed$ event which will emit whenever the request is either aborted, or a response has been sent back to the client. Here is a PR: #73898

fromEvent(request.raw.res, 'finish'), // Response is done
fromEvent(request.raw.req, 'close') // connection was closed
).pipe(shareReplay(1), first());
return {
Expand Down