socketio / socketio/socket.io

Issue with Socket.io Client using Next.js - Memory leak and JavaScript heap out of memory

Open
#4,922 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs investigation
Dominant language
TypeScript
Stars
63.2k
Forks
10.3k
Avg merge
11d 20h
Merged PRs (30d)
2

Description

Describe the bug
Having a issue with memory leak in my next.js application

To Reproduce

| [19:0x3378e60] 357862272 ms: Scavenge 1971.0 (2050.2) -> 1970.5 (2050.2) MB, 9.9 / 0.0 ms  (average mu = 0.364, current mu = 0.473) allocation failure                                                                                                                                                                                                        |
| [19:0x3378e60] 357862285 ms: Scavenge 1971.2 (2050.2) -> 1970.8 (2050.2) MB, 10.4 / 0.0 ms  (average mu = 0.364, current mu = 0.473) allocation failure                                                                                                                                                                                                       |
| [19:0x3378e60] 357862298 ms: Scavenge 1971.5 (2050.2) -> 1971.1 (2050.7) MB, 9.7 / 0.0 ms  (average mu = 0.364, current mu = 0.473) allocation failure                                                                                                                                                                                                        |
|     2: debug [0x38b14fe99f11] [/usr/src/app/node_modules/debug/src/common.js:~66] [pc=0x8b66a7fb143](this=0x3a24a65001b1 <JSGlobal Object>)                                                                                                                                                                                                                   |
|     3: arguments adaptor frame: 2->0                                                                                                                                                                                                                                                                                                                          |
|     4: /* anonymous */ [0x2860d0021f79] [file:///usr/src/app/node_modules/socket.io-client/build/esm-debug/manager.js:~337] [pc=0x8b66a59a21c](this=0x2860d...                                                                                                                                                                                                |
| FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory                                                                                                                                                                                                                                                      |
|  1: 0xa1ae50 node::Abort() [node]                                                                                                                                                                                                                                                                                                                             |
|  2: 0xa1b25c node::OnFatalError(char const*, char const*) [node]                                                                                                                                                                                                                                                                                              |
|  3: 0xb9b20e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]                                                                                                                                                                                                                                                                    |
|  4: 0xb9b589 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]                                                                                                                                                                                                                                                      |
|  5: 0xd58815  [node]                                                                                                                                                                                                                                                                                                                                          |
|  6: 0xd58ea6 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]                                                                                                                                                                                                                                                                       |
|  7: 0xd65765 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]                                                                                                                                                                                                                                         |
|  8: 0xd66615 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]                                                                                                                                                                                                             |
|  9: 0xd690cc v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]                                                                                                                                                                                      |
| 10: 0xd2faeb v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]                                                                                                                                                                                                                           |
| 11: 0x10720be v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]                                                                                                                                                                                                                                             |
| 12: 0x140e819  [node]                                                                                                                                                                                                                                                                                                                                         |
| [20:0x4a82e60] 358066299 ms: Scavenge 1988.6 (2061.7) -> 1983.0 (2066.2) MB, 32.2 / 0.1 ms  (average mu = 0.384, current mu = 0.485) allocation failure                                                                                                                                                                                                       |
| [20:0x4a82e60] 358066566 ms: Scavenge 1993.7 (2066.2) -> 1987.6 (2069.7) MB, 29.0 / 0.0 ms  (average mu = 0.384, current mu = 0.485) allocation failure                                                                                                                                                                                                       |
| [20:0x4a82e60] 358071582 ms: Mark-sweep 1998.3 (2069.7) -> 1953.1 (2075.0) MB, 4471.4 / 1.9 ms  (average mu = 0.353, current mu = 0.322) allocation failure scavenge might not succeed                                                                                                                                                                        |
| <--- JS stacktrace --->                                                                                                                                                                                                                                                                                                                                       |
| ==== JS stack trace =========================================                                                                                                                                                                                                                                                                                                 |
|     0: ExitFrame [pc: 0x140e819]                                                                                                                                                                                                                                                                                                                              |
| Security context: 0x20d6c9ec08d1 <JSObject>                                                                                                                                                                                                                                                                                                                   |
|     1: installListeners(aka installListeners) [0x3294e952f931] [_http_agent.js:~352] [pc=0xd60c2edc6e1](this=0x27ac0df804b1 <undefined>,0x1d9f9cee3f91 <Agent map = 0x3b6be68588f9>,0x1d9f9cee4b19 <Socket map = 0x3b6be6853309>,0x1d9f9cee4a09 <Object map = 0x904b77fd429>)                                                                                 |
|     2: addRequest [0x169037b7a3c1] [_http_agent.js:~219] [pc=0xd60c31e59c9](this=0x1d9f9cee3f91...                                                                                                                                                                                                                                                            |
| FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory                                                                                                                                                                                                                                                      |
|  1: 0xa1ae50 node::Abort() [node]                                                                                                                                                                                                                                                                                                                             |
|  2: 0xa1b25c node::OnFatalError(char const*, char const*) [node]                                                                                                                                                                                                                                                                                              |
|  3: 0xb9b20e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]                                                                                                                                                                                                                                                                    |
|  4: 0xb9b589 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]                                                                                                                                                                                                                                                      |
|  5: 0xd58815  [node]                                                                                                                                                                                                                                                                                                                                          |
|  6: 0xd58ea6 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]                                                                                                                                                                                                                                                                       |
|  7: 0xd65765 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]                                                                                                                                                                                                                                         |
|  8: 0xd66615 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]                                                                                                                                                                                                             |
|  9: 0xd690cc v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]                                                                                                                                                                                      |
| 10: 0xd2faeb v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]                                                                                                                                                                                                                           |
| 11: 0x10720be v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]                                                                                                                                                                                                                                             |
| 12: 0x140e819  [node]                                                                                                                                                                                                                                                                                                                                         |
| Aborted (core dumped)                                                                                                                                                                                                                                                                                                                                         |
| [20:0x3e22e60] 358309362 ms: Scavenge 1985.7 (2050.5) -> 1985.4 (2050.5) MB, 6.7 / 0.0 ms  (average mu = 0.223, current mu = 0.001) allocation failure                                                                                                                                                                                                        |
| [20:0x3e22e60] 358309372 ms: Scavenge 1986.0 (2050.5) -> 1985.7 (2050.7) MB, 6.7 / 0.0 ms  (average mu = 0.223, current mu = 0.001) allocation failure                                                                                                                                                                                                        |
| [20:0x3e22e60] 358312945 ms: Mark-sweep 1986.3 (2050.7) -> 1951.9 (2050.7) MB, 3570.0 / 1.0 ms  (average mu = 0.280, current mu = 0.333) allocation failure scavenge might not succeed                                                                                                                                                                        |
| <--- JS stacktrace --->                                                                                                                                                                                                                                                                                                                                       |
| ==== JS stack trace =========================================                                                                                                                                                                                                                                                                                                 |
|     0: ExitFrame [pc: 0x140e819]                                                                                                                                                                                                                                                                                                                              |
| Security context: 0x1b6c7f3008d1 <JSObject>                                                                                                                                                                                                                                                                                                                   |
|     1: new constructor(aka ClientRequest) [0x29eebc492b09] [_http_client.js:~85] [pc=0x31c56432a3e4](this=0x22c3189d7b81 <ClientRequest map = 0x2f7c5c0fe509>,0x22c3189d78a9 <Object map = 0x15a257d1c9f9>,0x1f6fb17004b1 <undefined>,0x1f6fb17004b1 <undefined>)                                                                                             |
|     2: ConstructFrame [pc: 0x138f8e0]                                                                                                                                                                                                                                                                                                                         |
|     3: initAsClient(aka initAsClient) [0x824837d9e1] [/usr/src/app/no...                                                                                                                                                                                                                                                                                      |
| FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory                                                                                                                                                                                                                                                      |
|  1: 0xa1ae50 node::Abort() [node]                                                                                                                                                                                                                                                                                                                             |
|  2: 0xa1b25c node::OnFatalError(char const*, char const*) [node]                                                                                                                                                                                                                                                                                              |
|  3: 0xb9b20e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]                                                                                                                                                                                                                                                                    |
|  4: 0xb9b589 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]                                                                                                                                                                                                                                                      |
|  5: 0xd58815  [node]                                                                                                                                                                                                                                                                                                                                          |
|  6: 0xd58ea6 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]                                                                                                                                                                                                                                                                       |
|  7: 0xd65765 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]                                                                                                                                                                                                                                         |
|  8: 0xd66615 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]                                                                                                                                                                                                             |
|  9: 0xd690cc v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]                                                                                                                                                                                      |
| 10: 0xd2fe5d v8::internal::Factory::NewFixedArrayWithFiller(v8::internal::RootIndex, int, v8::internal::Object, v8::internal::AllocationType) [node]                                                                                                                                                                                                          |
| 11: 0xd2ff50 v8::internal::Handle<v8::internal::FixedArray> v8::internal::Factory::NewFixedArrayWithMap<v8::internal::FixedArray>(v8::internal::RootIndex, int, v8::internal::AllocationType) [node]                                                                                                                                                          |
| 12: 0xf4ebd9 v8::internal::HashTable<v8::internal::NameDictionary, v8::internal::NameDictionaryShape>::New(v8::internal::Isolate*, int, v8::internal::AllocationType, v8::internal::MinimumCapacity) [node]                                                                                                                                                   |
| 13: 0xf5715e v8::internal::Dictionary<v8::internal::NameDictionary, v8::internal::NameDictionaryShape>::Add(v8::internal::Isolate*, v8::internal::Handle<v8::internal::NameDictionary>, v8::internal::Handle<v8::internal::Name>, v8::internal::Handle<v8::internal::Object>, v8::internal::PropertyDetails, int*) [node]                                     |
| 14: 0xf572c9 v8::internal::BaseNameDictionary<v8::internal::NameDictionary, v8::internal::NameDictionaryShape>::AddNoUpdateNextEnumerationIndex(v8::internal::Isolate*, v8::internal::Handle<v8::internal::NameDictionary>, v8::internal::Handle<v8::internal::Name>, v8::internal::Handle<v8::internal::Object>, v8::internal::PropertyDetails, int*) [node] |
| 15: 0xf597e3 v8::internal::BaseNameDictionary<v8::internal::NameDictionary, v8::internal::NameDictionaryShape>::Add(v8::internal::Isolate*, v8::internal::Handle<v8::internal::NameDictionary>, v8::internal::Handle<v8::internal::Name>, v8::internal::Handle<v8::internal::Object>, v8::internal::PropertyDetails, int*) [node]                             |
| 16: 0x1088bdc v8::internal::Runtime_AddDictionaryProperty(int, unsigned long*, v8::internal::Isolate*) [node]                                                                                                                                                                                                                                                 |
| 17: 0x140e819  [node]                       

All React Code

Socket.IO server version: 4.4.1

Server

const io = new Server(serverHTTP, {
  pingTimeout: 60000
});

Socket.IO client version: `x.y.z`

Client



const auth = {
  sessionID: sessao?.sessao_participante_id,
  type: "participante",
  token: process.env.NEXT_PUBLIC_TOKEN_WEBSOCKET,
  participante: user,
  webinar_id: data?.webinar_id,
  empresa_id: data?.empresa_id,
};

const socket = io(process.env.NEXT_PUBLIC_SOCKET_URL, {
  transports: ["websocket"],
  auth,
});

Expected behavior
Without memory

Platform:

  • Chrome
  • Safari

Additional context
Add any other context about the problem here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported heap growth in the Next.js application and trace the stack through socket.io-client/build/esm-debug/manager.js, debug/src/common.js, and Node's _http_agent.js. The issue is complete when the source of the leak is isolated, memory remains bounded during the reproduction, and a regression test or reproducible case demonstrates the result.

Written by the indexing model from the issue text.

Assessment

Tech stack
next.js, node.js, typescript
Domain
performance, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.