-
-
Notifications
You must be signed in to change notification settings - Fork 152
Fix: High unix socket concurrency #761
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
base: main
Are you sure you want to change the base?
Changes from all commits
362c689
362fa0e
82a6efb
cb09e5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // @vitest-environment node | ||
| import { it, expect, beforeAll, afterAll } from 'vitest' | ||
| import http from 'node:http' | ||
| import path from 'node:path' | ||
| import { promisify } from 'node:util' | ||
| import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest' | ||
| import { waitForClientRequest } from '../../../helpers' | ||
|
|
||
| const HTTP_SOCKET_PATH = path.join(__dirname, './test-early-return.sock') | ||
|
|
||
| const httpServer = http.createServer((req, res) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
| res.writeHead(200) | ||
| res.end('ok') | ||
| }) | ||
|
|
||
| const interceptor = new ClientRequestInterceptor() | ||
|
|
||
| beforeAll(async () => { | ||
| await new Promise<void>((resolve) => { | ||
| httpServer.listen(HTTP_SOCKET_PATH, resolve) | ||
| }) | ||
| interceptor.apply() | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| interceptor.dispose() | ||
| await promisify(httpServer.close.bind(httpServer))() | ||
| }) | ||
|
|
||
| /** | ||
| * When no request listeners are registered, the interceptor should call | ||
| * passthrough() immediately without going through the full async machinery. | ||
| * This prevents timing issues with high-concurrency Unix socket requests. | ||
| * @see https://github.com/mswjs/interceptors/issues/760 | ||
| */ | ||
| it('performs passthrough over a Unix socket when no request listeners are attached', async () => { | ||
| const request = http.request({ | ||
| socketPath: HTTP_SOCKET_PATH, | ||
| path: '/resource', | ||
| }) | ||
| request.end() | ||
| const { res, text } = await waitForClientRequest(request) | ||
|
|
||
| expect(res.statusCode).toBe(200) | ||
| await expect(text()).resolves.toBe('ok') | ||
| }) | ||
|
|
||
| it('handles concurrent Unix socket requests when no listeners are attached', async () => { | ||
| const requests = Array.from({ length: 20 }, () => { | ||
| const req = http.request({ | ||
| socketPath: HTTP_SOCKET_PATH, | ||
| path: '/resource', | ||
| }) | ||
| req.end() | ||
| return waitForClientRequest(req) | ||
| }) | ||
|
|
||
| const results = await Promise.all(requests) | ||
|
|
||
| for (const { res, text } of results) { | ||
| expect(res.statusCode).toBe(200) | ||
| await expect(text()).resolves.toBe('ok') | ||
| } | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do I remember it right that listening to the
request.on('socket')won't give you any TLS properties untilsecureConnectis emitted on the socket? Is that the correct way to access those properties (likesocket.encrypted)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MockHttpSocketprovides mock TLS values in its constructor so they're available immediately. For passthrough, mock values remain accessible until they're replaced with real values onsecureConnectRegarding the use of
Reflect.get, my understanding is that it's been used to avoid TypeScript issues.createConnection()returnsnet.Socket(which doesn't have anencrypted), but at runtime this is actually aTLSSocket. We could potentially cast toTLSSockethere and access it assocket.encrypted? It's a slightly more type-safe approach