Repository navigation
TS 5.1.3 Regression: TSC Compiler throws FATAL ERROR: Reached heap limit Allocation failed #54491
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptCrashFor flagging bugs which are compiler or service crashes or unclean exits, rather than bad outputFor flagging bugs which are compiler or service crashes or unclean exits, rather than bad outputFix AvailableA PR has been opened for this issueA PR has been opened for this issueRecent RegressionThis is a new regression just found in the last major/minor version of TypeScript.This is a new regression just found in the last major/minor version of TypeScript.
Milestone
Description
Bug Report
🔎 Search Terms
tsc, compiler, heap limit, crash, memory
🕗 Version & Regression Information
- This is a crash
- This changed between versions 5.0.4 and 5.1.3
⏯ Repository Link
https://github.com/virtuallyunknown/tsc-error
💻 Code
To reproduce the error, clone the repository and run the build script or just npx tsc.
git clone https://github.com/virtuallyunknown/tsc-error.git && cd tsc-error
npm install
npx tsc
🙁 Actual behavior
Running the typescript compiler (tsc) from project root causes the build to fail and produce this error:
<--- Last few GCs --->
[43355:0x6bd0ae0] 9563 ms: Scavenge 4038.1 (4116.6) -> 4037.2 (4117.8) MB, 4.5 / 0.0 ms (average mu = 0.538, current mu = 0.113) allocation failure;
[43355:0x6bd0ae0] 9572 ms: Scavenge 4039.0 (4117.8) -> 4038.2 (4138.6) MB, 8.4 / 0.0 ms (average mu = 0.538, current mu = 0.113) allocation failure;
[43355:0x6bd0ae0] 10371 ms: Mark-Compact 4052.2 (4138.6) -> 4048.8 (4152.1) MB, 794.9 / 0.0 ms (average mu = 0.390, current mu = 0.040) allocation failure; scavenge might not succeed
<--- JS stacktrace --->
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
1: 0xbf9890 node::Abort() [node]
2: 0xaf6859 [node]
3: 0xddaf80 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [node]
4: 0xddb336 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [node]
5: 0xfd9db5 [node]
6: 0xfed4e5 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
7: 0xfc924f v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
8: 0xfca297 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
9: 0xfa99fa v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
10: 0x13bd76f v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
11: 0x18443f9 [node]
Aborted (core dumped)
I also tried typescript@next, but that doesn't fix it. I also tried this command:
NODE_OPTIONS=--max-old-space-size=28000 npx tsc --noEmit --diagnostics --extendedDiagnostics --incremental false
but it crashes with a different error after a while.
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
🙂 Expected behavior
Running the typescript compiler (tsc) command compiles the build successfully after 1-2 seconds, just like it did in 5.0.4.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptCrashFor flagging bugs which are compiler or service crashes or unclean exits, rather than bad outputFor flagging bugs which are compiler or service crashes or unclean exits, rather than bad outputFix AvailableA PR has been opened for this issueA PR has been opened for this issueRecent RegressionThis is a new regression just found in the last major/minor version of TypeScript.This is a new regression just found in the last major/minor version of TypeScript.
Activity
[-]TS 5.1.4 Regression: TSC Compiler throws FATAL ERROR: Reached heap limit Allocation failed[/-][+]TS 5.1.3 Regression: TSC Compiler throws FATAL ERROR: Reached heap limit Allocation failed[/+]zhiyan114 commentedon Jun 2, 2023
Can confirm. Not only does it crash, but it also cause the transpile time to increase by ~x6 before the crash happens.
Andarist commentedon Jun 2, 2023
bisected to #53672 , cc Anders Hejlsberg (@ahejlsberg)
Andarist commentedon Jun 2, 2023
The problem originates in
@sapphire/shapeshiftand itsArrayValidator. A reduced test case is this:You can use this TS playground to verify this. Just wait for a moment - you will see that the browser tab crashes.
I reduced this to the
lengthRangemethod but in the regression test you might also want to include other methods from this class from the samelength*"family". They all do complex things with arrays/tuples.belgattitude commentedon Jun 2, 2023
Might be related to #54348 as well ?Edit: nope that one is fixedahejlsberg commentedon Jun 2, 2023
The issue appears to be runaway type instantiation as the compiler is attempting to compute variance information. That logic relies on our various depth limiters to catch infinite recursion, but apparently that doesn't happen fast enough here.
A quick workaround for now is to change the declaration of the
Tupletype toThat is, introduce a sanity check that
Nis actually anumberand resolve toneverif not. This would seem unnecessary sinceNis constrained tonumber, but variance computation introduces special marker types that (by design) aren't related to anything else.fatcerberus commentedon Jun 2, 2023
Isolated tabs are a wonderful thing. I still remember the days when this kind of thing would have taken out the entire browser.
Still a bit alarming that any JS code is able to outright crash a process (and thereby cause a DoS), though.
fix(arrayvalidator): fixed runaway type instantiation with TypeScript…
favna commentedon Jun 4, 2023
Many thanks for the fix that we can apply Anders Hejlsberg (@ahejlsberg)! We'll release an update for @sapphire/shapeshift ASAP so I think this issue can be closed.
Edit: I have released @sapphire/shapeshift v3.9.2 that resolves this issue
virtuallyunknown commentedon Jun 4, 2023
You guys are legends for fixing this so quickly, thanks to everyone involved!
If my understanding is correct, we now wait for the next TS release where this is backported, or just use the nightly version?
JstnMcBrd commentedon Jun 4, 2023
I also encountered this issue in my node discord.js project after upgrading from TypeScript 5.0.4 to 5.1.3. Enabling the
skipLibCheckoption has resolved it for now. Looking forward to the fix!favna commentedon Jun 5, 2023
As an
@sapphire/shapeshiftupdate has been rolled out you can update the transitive dependency with your package manager of choice. Once you have updated the transitive dependency you should be able to removeskipLibCheck.npm
I cannot find anything for npm to do a transitive dependency update so you'll have to provide
overridesuntil@discordjs/buildersreleases an update (and subsequentlydiscord.js). If someone knows a way to update transitive dependencies with npm please do correct me.In your
package.json:{ "overrides": { "@sapphire/shapeshift": "3.9.2" } }Then run
npm install. You can verify the change by checking yourpackage-lock.jsonYarn v1
Yarn v1 (deprecated, recommend updating to v3) does not have a way to update transitive dependencies so a similar strategy is required as npm:
In your
package.json:{ "resolutions": { "@sapphire/shapeshift": "3.9.2" } }Then run
yarn install. You can verify the change by checking youryarn.lockYarn v3
Yarn v3 can update transitive dependencies so run the command:
pnpm
pnpm can also update transitive dependencies so run the command: