0

I need a while to verify if a generated id alredy exists in the db, so i made this code:

let ssid;
while ( ssid == undefined ) {
    let tempSId = assets.makeid(30);
    MongoClient.connect(mongoUrl, function(err, db) {
        if (err) message.guild.owner.user.send(assets.err(err));
        var dbo = db.db(conf.mainDB.name);
        var query = { sid: tempSId };
        dbo.collection(conf.mainDB.tables[0]).find(query).toArray(function(err, result) {
        if (err) message.guild.owner.user.send(assets.err(err));
        if ( result[0] == undefined ) { ssid = tempSId; }
        db.close();
        });
    });
}

where i search for objects in the db with the same id. But it gives me this error:

[Admin Tools] > Online!
(node:22154) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.

<--- Last few GCs --->

[22154:0x49ca400]    44793 ms: Mark-sweep 2044.1 (2051.6) -> 2043.4 (2051.6) MB, 1441.8 / 0.8 ms  (average mu = 0.048, current mu = 0.008) allocation failure scavenge might not succeed


<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x13cf019]
    1: StubFrame [pc: 0x13d00b3]
Security context: 0x056c5c2c08d1 <JSObject>
    2: /* anonymous */ [0x3939aff47e49] [/home/ezzer29/node-apps/admin-tools-discord/main.js:~28] [pc=0x26120c4a55dd](this=0x1c40355822c9 <JSGlobal Object>,0x3ce30b9001b9 <null>,0x3939aff47e89 <JSArray[0]>)
    3: /* anonymous */(aka /* anonymous */) [0x3939aff47ea9] [/home/ezzer29/node-apps/admin-tools-discord/node_modules/mongod...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0xa093f0 node::Abort() [node]
 2: 0xa097fc node::OnFatalError(char const*, char const*) [node]
 3: 0xb842ae v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: 0xb84629 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xd30fe5  [node]
 6: 0xd31676 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]
 7: 0xd3def5 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
 8: 0xd3eda5 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 9: 0xd4185c v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
10: 0xd0830b v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
11: 0x1049f4e v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
12: 0x13cf019  [node]
Aborted (core dumped)
npm ERR! code ELIFECYCLE
npm ERR! errno 134
npm ERR! admin-tools@1.0.0 start: `node main.js`
npm ERR! Exit status 134
npm ERR! 
npm ERR! Failed at the admin-tools@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ezzer29/.npm/_logs/2020-09-13T13_48_17_874Z-debug.log

the query function works ffine on its own and so does the while but when i put them together it crashes.

Thanks!

  • Check out https://stackoverflow.com/questions/42308879/how-to-solve-npm-error-npm-err-code-elifecycle – Joe Sep 13 '20 at 17:07
  • Mine is not an error from npm, i think it has to do with the mongodb function being too slow – JB Reese9872 Sep 13 '20 at 17:17
  • There are a couple of errors in there `JavaScript heap out of memory`, `npm ERR! code ELIFECYCLE`. The `DeprecationWarning` from the driver is just a warning, and I don't see any other messages from the driver. Does that noted debug log contain anything useful? – Joe Sep 13 '20 at 17:22
  • Not really, i can send it if u want – JB Reese9872 Sep 13 '20 at 19:06

1 Answers1

0

That stack trace shows FatalProcessOutOfMemory was called from within Runtime_AllocateInYoungGeneration, both of which are in the v8 module.

The MongoClient.connect function is asynchronous, which is why it takes a callback. Essentially the connect function begins the process of connecting to the server, and returns immediately. When the connection finishes establishing or fails, it then passes the error or database to the callback.

What is happening in this case, is the while loop is creating connections as fast as it possibly can, without waiting for the callback to resolve.

The result is the local machine has so many connections, buffers, and closures around the callback that is running out of memory before anything resolves.

Try putting the while loop inside the connect callback instead of the other way around.

Joe
  • 13,504
  • 1
  • 13
  • 31
  • thx, i figured its something like that, but had no idea how to fix it :) – JB Reese9872 Sep 15 '20 at 14:26
  • so, i havent used that back in sept cuz my drive got corrupted and i lost all the files, but i needed it into a project now and i used the solution u said above and it didnt work, it gives me the same error :{ – JB Reese9872 Oct 04 '20 at 20:08