Skip to content

Mongoose 8.19.x causes TypeScript to run out of memory when passing a generic type to mongoose.model #15681

@maxwillkelly

Description

@maxwillkelly

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.19.1

Node.js version

22.19.0

MongoDB server version

6.20.0

Typescript version (if applicable)

5.9.3

Description

When passing a generic type to mongoose.model with mongoose 8.19.0 or 8.19.1, the TypeScript heap runs out of memory.

Downgrading to mongoose 8.18.3 fixes this issue.

<--- Last few GCs --->

[50586:0x138008000]    47547 ms: Mark-Compact 4047.6 (4138.5) -> 4033.2 (4139.7) MB, pooled: 0 MB, 422.00 / 0.00 ms  (average mu = 0.073, current mu = 0.034) allocation failure; scavenge might not succeed
[50586:0x138008000]    48074 ms: Mark-Compact 4048.9 (4139.7) -> 4034.5 (4141.0) MB, pooled: 0 MB, 510.62 / 0.00 ms  (average mu = 0.050, current mu = 0.030) allocation failure; scavenge might not succeed


<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
----- Native stack trace -----

 1: 0x1002930b8 node::OOMErrorHandler(char const*, v8::OOMDetails const&) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
 2: 0x10046eb70 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, v8::OOMDetails const&) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
 3: 0x10067e298 v8::internal::Heap::stack() [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
 4: 0x100694710 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags)::$_1::operator()() const [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
 5: 0x100693fa4 void heap::base::Stack::SetMarkerAndCallbackImpl<v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags)::$_1>(heap::base::Stack*, void*, void const*) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
 6: 0x100e4e150 PushAllRegistersAndIterateStack [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
 7: 0x10067c458 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
 8: 0x100670c20 v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
 9: 0x100671458 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
10: 0x100654778 v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
11: 0x100a70228 v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
12: 0x100f31af4 Builtins_CEntry_Return1_ArgvOnStack_NoBuiltinExit [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
13: 0x107663ad4 
14: 0x10761bd3c 
15: 0x10769ce48 
16: 0x1075cd8e0 
17: 0x107675280 
18: 0x10769cfec 
19: 0x1075aea10 
20: 0x1075f4ea0 
21: 0x10765bc54 
22: 0x10766ae94 
23: 0x1075b4f00 
24: 0x10722d864 
25: 0x1075f75b8 
26: 0x1075db984 
27: 0x10765ba04 
28: 0x10766ae94 
29: 0x1075b4f00 
30: 0x1075f8414 
31: 0x1075db984 
32: 0x10765ba04 
33: 0x10766ae94 
34: 0x1075b4f00 
35: 0x10765be30 
36: 0x10766ae94 
37: 0x1075b4f00 
38: 0x1075f8414 
39: 0x10720c4b8 
40: 0x10765ba04 
41: 0x10766ae94 
42: 0x1075b4f00 
43: 0x10765be30 
44: 0x10766ae94 
45: 0x1075b4f00 
46: 0x1076584d8 
47: 0x10722b058 
48: 0x10720c4b8 
49: 0x10765ba04 
50: 0x10766ae94 
51: 0x1075b4f00 
52: 0x1071e7388 
53: 0x10766ae94 
54: 0x1075b4f00 
55: 0x10722c644 
56: 0x10722b058 
57: 0x10720c4b8 
58: 0x1071e704c 
59: 0x10766ae94 
60: 0x1075b4f00 
61: 0x1071e7388 
62: 0x10766ae94 
63: 0x1075b4f00 
64: 0x10722c644 
65: 0x10722b058 
66: 0x10720c4b8 
67: 0x1071e704c 
68: 0x10766ae94 
69: 0x1075b4f00 
70: 0x1071e7388 
71: 0x10766ae94 
72: 0x1075b4f00 
73: 0x10722c644 
74: 0x10722b058 
75: 0x10720c4b8 
76: 0x1071e704c 
77: 0x10766ae94 
78: 0x1075b4f00 
79: 0x1071e7388 
80: 0x10766ae94 
81: 0x1075b4f00 
82: 0x10722c644 
83: 0x10722b058 
84: 0x10720c4b8 
85: 0x1071e704c 
86: 0x10766ae94 
87: 0x1075b4f00 
88: 0x107626f6c 
89: 0x10721312c 
90: 0x1071e704c 
91: 0x10766ae94 
92: 0x1075b4f00 
93: 0x10722c644 
94: 0x10722b058 
95: 0x1075b4e84 
96: 0x107211a0c 
97: 0x1071e704c 
98: 0x10766ae94 
99: 0x1075b4f00 
100: 0x10722d864 
101: 0x10722b1c8 
102: 0x10720c4b8 
103: 0x1071e704c 
104: 0x10766ae94 
105: 0x1075b4f00 
106: 0x10722c644 
107: 0x10722b058 
108: 0x10720c4b8 
109: 0x1071e704c 
110: 0x10766ae94 
111: 0x1075b4f00 
112: 0x107624e9c 
113: 0x10764be70 
114: 0x10726d650 
115: 0x1071d9230 
116: 0x107212ef4 
117: 0x1071e704c 
118: 0x10766ae94 
119: 0x1075b4f00 
120: 0x107318874 
121: 0x107635668 
122: 0x107212dec 
123: 0x1071e704c 
124: 0x10766ae94 
125: 0x1075b4f00 
126: 0x10722d864 
127: 0x10722b1c8 
128: 0x10720c4b8 
129: 0x1071e704c 
130: 0x10766ae94 
131: 0x1075b4f00 
132: 0x107325d14 
133: 0x1070368ac 
134: 0x10724ba38 
135: 0x1071c8b54 
136: 0x1071de4b0 
137: 0x10726f5b4 
138: 0x1071d4e74 
139: 0x1072369cc 
140: 0x1071ceea0 
141: 0x1071cdf94 
142: 0x10717003c 
143: 0x10730d784 
144: 0x10706aa14 
145: 0x1072560fc 
146: 0x107167030 
147: 0x106f9af84 
148: 0x106f26e40 
149: 0x10766fb2c 
150: 0x107217eac 
151: 0x107256bfc 
152: 0x107069e24 
153: 0x1071a9968 
154: 0x107026108 
155: 0x107256ed8 
156: 0x10725705c 
157: 0x1070699ec 
158: 0x1071a9968 
159: 0x107026234 
160: 0x1071ea494 
161: 0x1070699a4 
162: 0x1071a9968 
163: 0x1075963e4 
164: 0x10759825c 
165: 0x10766df80 
166: 0x107598404 
167: 0x10706995c 
168: 0x1071a9968 
169: 0x107026234 
170: 0x1072f0e18 
171: 0x1072f284c 
172: 0x1072f2ad8 
173: 0x1072f5b78 
174: 0x1072f2c1c 
175: 0x10719f13c 
176: 0x1071a02c0 
177: 0x10719f7e8 
178: 0x10719fab4 
179: 0x10719fd1c 
180: 0x10719feb8 
181: 0x1075ce170 
182: 0x106e78d18 
183: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
184: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
185: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
186: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
187: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
188: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
189: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
190: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
191: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
192: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
193: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
194: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
195: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
196: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
197: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
198: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
199: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
200: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
201: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
202: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
203: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
204: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
205: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
206: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
207: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
208: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
209: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
210: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
211: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
212: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
213: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
214: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
215: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
216: 0x100e9cef0 Builtins_InterpreterEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
217: 0x100e9ac0c Builtins_JSEntryTrampoline [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
218: 0x100e9a8f4 Builtins_JSEntry [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
219: 0x1005d6194 v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
220: 0x1005d5af0 v8::internal::Execution::Call(v8::internal::Isolate*, v8::internal::Handle<v8::internal::Object>, v8::internal::Handle<v8::internal::Object>, int, v8::internal::Handle<v8::internal::Object>*) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
221: 0x1004850dc v8::Function::Call(v8::Local<v8::Context>, v8::Local<v8::Value>, int, v8::Local<v8::Value>*) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
222: 0x10026d058 node::builtins::BuiltinLoader::CompileAndCall(v8::Local<v8::Context>, char const*, node::Realm*) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
223: 0x100325b00 node::Realm::ExecuteBootstrapper(char const*) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
224: 0x10024abac node::StartExecution(node::Environment*, char const*) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
225: 0x10024ab00 node::StartExecution(node::Environment*, std::__1::function<v8::MaybeLocal<v8::Value> (node::StartExecutionCallbackInfo const&)>) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
226: 0x1001963b8 node::LoadEnvironment(node::Environment*, std::__1::function<v8::MaybeLocal<v8::Value> (node::StartExecutionCallbackInfo const&)>, std::__1::function<void (node::Environment*, v8::Local<v8::Value>, v8::Local<v8::Value>)>) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
227: 0x1002dddcc node::NodeMainInstance::Run() [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
228: 0x10024dfc0 node::Start(int, char**) [/Users/maxkelly/.local/share/fnm/node-versions/v22.19.0/installation/bin/node]
229: 0x199032b98 start [/usr/lib/dyld]

Steps to Reproduce

index.ts

import mongoose from "mongoose";

function getMongooseModel<T>(resName: string): mongoose.Model<T> {
  if (mongoose.models[resName]) {
    return mongoose.models[resName];
  }

  const schema = new mongoose.Schema({
    example: String,
  });

  return mongoose.model<T>(resName, schema);
}

type Example = {
  name: string;
};

const Model = getMongooseModel<Example>("Example");

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "moduleDetection": "force",
    "moduleResolution": "Bundler",
    "allowImportingTsExtensions": true,
    "customConditions": ["module"],
    "allowArbitraryExtensions": true,
    "noEmit": true,
    "allowJs": true,
    "esModuleInterop": true,
    "inlineSourceMap": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "outDir": "./lib",
    "baseUrl": "."
  },
  "include": ["index.ts"],
  "exclude": ["node_modules"]
}

Expected Behavior

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions