Unverified Commit 9a4d31f1 authored by Josh Story's avatar Josh Story Committed by GitHub

Only enable unhandledRejection filtering when opted in (#83726)

We need to debug some unexpected process crashes in front. This change
makes the unhandled rejection filter only enabled when configred with
the __NEXT_USE_UNHANDLED_REJECTION_FILTER environment variable.
parent 4d26abdd
...@@ -21,6 +21,48 @@ ...@@ -21,6 +21,48 @@
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external' import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
const MODE:
| 'enabled'
| 'debug'
| 'true'
| 'false'
| '1'
| '0'
| ''
| string
| undefined = process.env.__NEXT_USE_UNHANDLED_REJECTION_FILTER
let ENABLE_UHR_FILTER = false
let DEBUG_UHR_FILTER = false
switch (MODE) {
case 'debug':
DEBUG_UHR_FILTER = true
// fallthrough
case 'enabled':
case 'true':
case '1':
ENABLE_UHR_FILTER = true
break
case 'false':
case 'disabled':
case '0':
case '':
case undefined:
break
default:
if (typeof MODE === 'string') {
console.error(
`__NEXT_USE_UNHANDLED_REJECTION_FILTER has an unrecognized value: ${JSON.stringify(MODE)}. Use "enabled", "disabled", or "debug" or omit the environment variable altogether`
)
}
}
const debug = DEBUG_UHR_FILTER
? (...args: any[]) =>
console.log('[UNHANDLED REJECTION DEBUG]', ...args, new Error().stack)
: undefined
type ListenerMetadata = { type ListenerMetadata = {
listener: NodeJS.UnhandledRejectionListener listener: NodeJS.UnhandledRejectionListener
once: boolean once: boolean
...@@ -55,9 +97,15 @@ let didWarnRemoveAll = false ...@@ -55,9 +97,15 @@ let didWarnRemoveAll = false
*/ */
function installUnhandledRejectionFilter(): void { function installUnhandledRejectionFilter(): void {
if (filterInstalled) { if (filterInstalled) {
debug?.('unexpected second install')
return return
} }
debug?.(
'installUnhandledRejectionFilter',
process.listeners('unhandledRejection').map((l) => l.toString())
)
// Capture existing handlers // Capture existing handlers
underlyingListeners = Array.from(process.listeners('unhandledRejection')) underlyingListeners = Array.from(process.listeners('unhandledRejection'))
// We assume all existing handlers are not "once" // We assume all existing handlers are not "once"
...@@ -94,6 +142,7 @@ function installUnhandledRejectionFilter(): void { ...@@ -94,6 +142,7 @@ function installUnhandledRejectionFilter(): void {
listener: (...args: any[]) => void listener: (...args: any[]) => void
) { ) {
if (event === 'unhandledRejection') { if (event === 'unhandledRejection') {
debug?.('process.on/addListener', listener.toString())
// Add new handlers to our internal queue instead of the process // Add new handlers to our internal queue instead of the process
underlyingListeners.push(listener as NodeJS.UnhandledRejectionListener) underlyingListeners.push(listener as NodeJS.UnhandledRejectionListener)
listenerMetadata.push({ listener, once: false }) listenerMetadata.push({ listener, once: false })
...@@ -115,6 +164,7 @@ function installUnhandledRejectionFilter(): void { ...@@ -115,6 +164,7 @@ function installUnhandledRejectionFilter(): void {
listener: (...args: any[]) => void listener: (...args: any[]) => void
) { ) {
if (event === 'unhandledRejection') { if (event === 'unhandledRejection') {
debug?.('process.once', listener.toString())
underlyingListeners.push(listener) underlyingListeners.push(listener)
listenerMetadata.push({ listener, once: true }) listenerMetadata.push({ listener, once: true })
return process return process
...@@ -128,6 +178,7 @@ function installUnhandledRejectionFilter(): void { ...@@ -128,6 +178,7 @@ function installUnhandledRejectionFilter(): void {
originalProcessPrependListener, originalProcessPrependListener,
function (event: string | symbol, listener: (...args: any[]) => void) { function (event: string | symbol, listener: (...args: any[]) => void) {
if (event === 'unhandledRejection') { if (event === 'unhandledRejection') {
debug?.('process.prependListener', listener.toString())
if (didWarnPrepend === false) { if (didWarnPrepend === false) {
didWarnPrepend = true didWarnPrepend = true
console.warn( console.warn(
...@@ -156,6 +207,7 @@ function installUnhandledRejectionFilter(): void { ...@@ -156,6 +207,7 @@ function installUnhandledRejectionFilter(): void {
originalProcessPrependOnceListener, originalProcessPrependOnceListener,
function (event: string | symbol, listener: (...args: any[]) => void) { function (event: string | symbol, listener: (...args: any[]) => void) {
if (event === 'unhandledRejection') { if (event === 'unhandledRejection') {
debug?.('process.prependOnceListener', listener.toString())
if (didWarnPrepend === false) { if (didWarnPrepend === false) {
didWarnPrepend = true didWarnPrepend = true
console.warn( console.warn(
...@@ -183,6 +235,7 @@ function installUnhandledRejectionFilter(): void { ...@@ -183,6 +235,7 @@ function installUnhandledRejectionFilter(): void {
listener: (...args: any[]) => void listener: (...args: any[]) => void
) { ) {
if (event === 'unhandledRejection') { if (event === 'unhandledRejection') {
debug?.('process.removeListener', listener.toString())
// Check if they're trying to remove our filtering handler // Check if they're trying to remove our filtering handler
if (listener === filteringUnhandledRejectionHandler) { if (listener === filteringUnhandledRejectionHandler) {
uninstallUnhandledRejectionFilter() uninstallUnhandledRejectionFilter()
...@@ -191,8 +244,11 @@ function installUnhandledRejectionFilter(): void { ...@@ -191,8 +244,11 @@ function installUnhandledRejectionFilter(): void {
const index = underlyingListeners.lastIndexOf(listener) const index = underlyingListeners.lastIndexOf(listener)
if (index > -1) { if (index > -1) {
debug?.('process.removeListener match found', index)
underlyingListeners.splice(index, 1) underlyingListeners.splice(index, 1)
listenerMetadata.splice(index, 1) listenerMetadata.splice(index, 1)
} else {
debug?.('process.removeListener match not found', index)
} }
return process return process
} }
...@@ -211,6 +267,10 @@ function installUnhandledRejectionFilter(): void { ...@@ -211,6 +267,10 @@ function installUnhandledRejectionFilter(): void {
originalProcessRemoveAllListeners, originalProcessRemoveAllListeners,
function (event?: string | symbol) { function (event?: string | symbol) {
if (event === 'unhandledRejection') { if (event === 'unhandledRejection') {
debug?.(
'process.removeAllListeners',
underlyingListeners.map((l) => l.toString())
)
if (didWarnRemoveAll === false) { if (didWarnRemoveAll === false) {
didWarnRemoveAll = true didWarnRemoveAll = true
console.warn( console.warn(
...@@ -245,6 +305,12 @@ function installUnhandledRejectionFilter(): void { ...@@ -245,6 +305,12 @@ function installUnhandledRejectionFilter(): void {
event: string | symbol event: string | symbol
) { ) {
if (event === 'unhandledRejection') { if (event === 'unhandledRejection') {
debug?.(
'process.listeners',
[filteringUnhandledRejectionHandler, ...underlyingListeners].map((l) =>
l.toString()
)
)
return [filteringUnhandledRejectionHandler, ...underlyingListeners] return [filteringUnhandledRejectionHandler, ...underlyingListeners]
} }
return originalProcessListeners.call(process, event as any) return originalProcessListeners.call(process, event as any)
...@@ -263,6 +329,18 @@ function installUnhandledRejectionFilter(): void { ...@@ -263,6 +329,18 @@ function installUnhandledRejectionFilter(): void {
originalProcessOn.call(process, 'rejectionHandled', noopRejectionHandled) originalProcessOn.call(process, 'rejectionHandled', noopRejectionHandled)
filterInstalled = true filterInstalled = true
debug?.(
'after install actual listeners',
originalProcessListeners
.call(process, 'unhandledRejection' as any)
.map((l) => l.toString())
)
debug?.(
'after install listeners',
process.listeners('unhandledRejection').map((l) => l.toString())
)
} }
/** /**
...@@ -272,9 +350,17 @@ function installUnhandledRejectionFilter(): void { ...@@ -272,9 +350,17 @@ function installUnhandledRejectionFilter(): void {
*/ */
function uninstallUnhandledRejectionFilter(): void { function uninstallUnhandledRejectionFilter(): void {
if (!filterInstalled) { if (!filterInstalled) {
debug?.('unexpected second uninstall')
return return
} }
debug?.(
'uninstallUnhandledRejectionFilter',
[filteringUnhandledRejectionHandler, ...underlyingListeners].map((l) =>
l.toString()
)
)
// Restore original process methods // Restore original process methods
process.on = originalProcessOn process.on = originalProcessOn
process.addListener = originalProcessAddListener process.addListener = originalProcessAddListener
...@@ -312,6 +398,11 @@ function uninstallUnhandledRejectionFilter(): void { ...@@ -312,6 +398,11 @@ function uninstallUnhandledRejectionFilter(): void {
filterInstalled = false filterInstalled = false
underlyingListeners.length = 0 underlyingListeners.length = 0
listenerMetadata.length = 0 listenerMetadata.length = 0
debug?.(
'after uninstall',
process.listeners('unhandledRejection').map((l) => l.toString())
)
} }
/** /**
...@@ -385,4 +476,6 @@ function filteringUnhandledRejectionHandler( ...@@ -385,4 +476,6 @@ function filteringUnhandledRejectionHandler(
function noopRejectionHandled() {} function noopRejectionHandled() {}
// Install the filter when this module is imported // Install the filter when this module is imported
installUnhandledRejectionFilter() if (ENABLE_UHR_FILTER) {
installUnhandledRejectionFilter()
}
...@@ -10,6 +10,9 @@ describe('Cache Components Errors', () => { ...@@ -10,6 +10,9 @@ describe('Cache Components Errors', () => {
files: __dirname + '/fixtures/default', files: __dirname + '/fixtures/default',
skipStart: !isNextDev, skipStart: !isNextDev,
skipDeployment: true, skipDeployment: true,
env: {
__NEXT_USE_UNHANDLED_REJECTION_FILTER: 'enabled',
},
}) })
const isRspack = !!process.env.NEXT_RSPACK const isRspack = !!process.env.NEXT_RSPACK
......
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