Unverified Commit cac9ccf9 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure proxy rewrite does not hang on error (#24394)

* Ensure proxy rewrite does not hang on error

* remove logs

* Listen to req close
parent cf4ba8d7
...@@ -924,12 +924,29 @@ export default class Server { ...@@ -924,12 +924,29 @@ export default class Server {
target, target,
changeOrigin: true, changeOrigin: true,
ignorePath: true, ignorePath: true,
proxyTimeout: 30_000, // limit proxying to 30 seconds
}) })
proxy.web(req, res)
proxy.on('error', (err: Error) => { await new Promise((proxyResolve, proxyReject) => {
console.error(`Error occurred proxying ${target}`, err) let finished = false
proxy.on('proxyReq', (proxyReq) => {
proxyReq.on('close', () => {
if (!finished) {
finished = true
proxyResolve(true)
}
})
})
proxy.on('error', (err) => {
if (!finished) {
finished = true
proxyReject(err)
}
})
proxy.web(req, res)
}) })
return { return {
finished: true, finished: true,
} }
......
...@@ -11,6 +11,10 @@ module.exports = { ...@@ -11,6 +11,10 @@ module.exports = {
}, },
] ]
: []), : []),
{
source: '/to-nowhere',
destination: 'http://localhost:12233',
},
{ {
source: '/rewriting-to-auto-export', source: '/rewriting-to-auto-export',
destination: '/auto-export/hello?rewrite=1', destination: '/auto-export/hello?rewrite=1',
......
...@@ -38,6 +38,14 @@ let appPort ...@@ -38,6 +38,14 @@ let appPort
let app let app
const runTests = (isDev = false) => { const runTests = (isDev = false) => {
it('should not hang when proxy rewrite fails', async () => {
const res = await fetchViaHTTP(appPort, '/to-nowhere', undefined, {
timeout: 5000,
})
expect(res.status).toBe(500)
})
it('should parse params correctly for rewrite to auto-export dynamic page', async () => { it('should parse params correctly for rewrite to auto-export dynamic page', async () => {
const browser = await webdriver(appPort, '/rewriting-to-auto-export') const browser = await webdriver(appPort, '/rewriting-to-auto-export')
const text = await browser.eval(() => document.documentElement.innerHTML) const text = await browser.eval(() => document.documentElement.innerHTML)
...@@ -1395,6 +1403,11 @@ const runTests = (isDev = false) => { ...@@ -1395,6 +1403,11 @@ const runTests = (isDev = false) => {
}, },
], ],
afterFiles: [ afterFiles: [
{
destination: 'http://localhost:12233',
regex: normalizeRegEx('^\\/to-nowhere$'),
source: '/to-nowhere',
},
{ {
destination: '/auto-export/hello?rewrite=1', destination: '/auto-export/hello?rewrite=1',
regex: normalizeRegEx('^\\/rewriting-to-auto-export$'), regex: normalizeRegEx('^\\/rewriting-to-auto-export$'),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment