AmyrAhmady / samp-node

a SA-MP plugin to run nodejs scripts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Promise cause server crashed

rsetiawan7 opened this issue · comments

Server version: 0.3.7 R2

samp-node version: 1.1.0

Issue:

  • Promise causes server crashed

server.cfg

echo loading server.cfg generated by sampctl - do not edit this file by hand.
gamemode0 test
plugins samp-node
rcon_password hehehehe
port 8192
hostname SA-MP Server
maxplayers 5
language 
mapname San Andreas
weburl www.sa-mp.com
gamemodetext Mini Mission
announce 0
lanmode 0
query 1
rcon 0
logqueries 0
sleep 5
maxnpc 0
stream_rate 1000
stream_distance 200.000000
onfoot_rate 30
incar_rate 30
weapon_rate 30
chatlogging 1
timestamp 1
messageholelimit 3000
messageslimit 500
ackslimit 3000
playertimeout 10000
minconnectiontime 0
lagcompmode 1
connseedtime 300000
db_logging 0
db_log_queries 0
conncookies 1
cookielogging 0
output 1

samp-node.json

{
  "log_level": 4,
  "entry_file": "index.js",
  "enable_resources": false
}

test.pwn

#include <a_samp>

main()
{
  return 1;
}

public OnGameModeInit()
{
  return 1;
}

public OnGameModeExit()
{
  return 1;
}

index.js

const Sequelize = require('sequelize');

const {
  OnGameModeInit,
  OnGameModeExit,
} = require('samp-node-lib');

const sequelize = new Sequelize({
  dialect: 'mariadb',
  host: 'localhost',
  username: 'root',
  database: 'samp',
  dialectOptions: {
    timezone: 'Etc/GMT+7'
  }
});

OnGameModeInit(() => {
  console.log('print here');

  sequelize.sync()
    .then(() => console.log('Connected to the database.'))
    .catch(e => console.error('An error occured while trying to connect to database.', e));
});

OnGameModeExit(() => {
  sequelize.close()
    .then(() => console.log('Connection closed'))
    .catch(e => console.error('Failed to close connection', e));
})

server-log.txt

----------
Loaded log file: "server_log.txt".
----------

SA-MP Dedicated Server
----------------------
v0.3.7-R2, (C)2005-2015 SA-MP Team

language = ""  (string)

Server Plugins
--------------
 Loading plugin: samp-node        
  Loaded.
 Loaded 1 plugins.


Started server on port: 8192, with maxplayers: 5 lanmode is OFF.


Filterscripts
---------------
  Loaded 0 filterscripts.

print here
Number of vehicle models: 0
failed to run package: received runtime error: failed to start server: exit status 3221225477

samp-node.log

[06/11/2020 - 08:38:36] -> [PLUGIN] samp-node plugin started...
[06/11/2020 - 08:38:36] -> [DEBUG]: node flags: node
[06/11/2020 - 08:38:36] -> [DEBUG]: node flags: index.js

crashinfo.txt

--------------------------

SA-MP Server: 0.3.7-R2



Exception At Address: 0x7887454D Module: (node.dll)



Registers:

EAX: 0x0019E2F0	EBX: 0x00000000	ECX: 0x0019E2F0	EDX: 0x0019E36A

ESI: 0x00855160	EDI: 0x0019EBB8	EBP: 0x0019E310	ESP: 0x0019E2D0

EFLAGS: 0x00010246



Stack:

+0000: 0x0019EBB8   0x00855160   0x00899B38   0x00000000

+0010: 0x00000000   0x00000000   0x00000000   0x00000000

+0020: 0x00000000   0x784A427B   0x00000000   0x00000000

+0030: 0x00000000   0x00000000   0x00000000   0x00000000

+0040: 0x0019EB9C   0x784B4187   0x0019E32C   0x0089662C

+0050: 0x0019E350   0xFFFFFFFF   0x00899B38   0x795DEDBC

+0060: 0x00855160   0x0089662C   0x00897608   0x784DF850

+0070: 0x78874510   0x0089662C   0x0019E78C   0x7721621D

+0080: 0x73206F4E   0x20686375   0x75646F6D   0x203A656C

+0090: 0x65726163   0x72775F73   0x00007061   0x00810000

+00A0: 0x00855160   0x00852414   0x00000064   0x00000000

+00B0: 0x030F3358   0x00000003   0x00000064   0x00000000

+00C0: 0x00839680   0x00000022   0x00000000   0x00200001

+00D0: 0x0083DF58   0x0083E6F8   0x00839670   0x00810000

+00E0: 0x00000000   0x0019E3D0   0x771D8786   0x00000000

+00F0: 0x00000000   0x00000000   0x00000000   0x0083E6F8

+0100: 0x0019E3E4   0x76183C9B   0x00810000   0x00000000

+0110: 0x0083E6F8   0x0019E3F4   0x76183C68   0x0083E6F8

+0120: 0x00000000   0x0019E4A0   0x78EA2A67   0x0083E6F8

+0130: 0x0019E58C   0x789CA8EB   0x0019E4A8   0x00000000



--------------------------



Loaded Modules:

samp-server.exe	A: 0x00400000 - 0x00519000	(C:\Users\sheen\Projects\SA-MP\samp-mini-mission\samp-server.exe)

ntdll.dll	A: 0x77190000 - 0x77333000	(C:\Windows\SYSTEM32\ntdll.dll)

KERNEL32.DLL	A: 0x76850000 - 0x76940000	(C:\Windows\System32\KERNEL32.DLL)

KERNELBASE.dll	A: 0x756E0000 - 0x758F4000	(C:\Windows\System32\KERNELBASE.dll)

SHELL32.dll	A: 0x76B20000 - 0x770D2000	(C:\Windows\System32\SHELL32.dll)

msvcp_win.dll	A: 0x752E0000 - 0x7535B000	(C:\Windows\System32\msvcp_win.dll)

ucrtbase.dll	A: 0x76150000 - 0x76270000	(C:\Windows\System32\ucrtbase.dll)

USER32.dll	A: 0x76270000 - 0x76406000	(C:\Windows\System32\USER32.dll)

win32u.dll	A: 0x76990000 - 0x769A8000	(C:\Windows\System32\win32u.dll)

GDI32.dll	A: 0x750B0000 - 0x750D3000	(C:\Windows\System32\GDI32.dll)

gdi32full.dll	A: 0x75200000 - 0x752DA000	(C:\Windows\System32\gdi32full.dll)

WINMM.dll	A: 0x718A0000 - 0x718C8000	(C:\Windows\SYSTEM32\WINMM.dll)

WSOCK32.dll	A: 0x74420000 - 0x74428000	(C:\Windows\SYSTEM32\WSOCK32.dll)

msvcrt.dll	A: 0x75360000 - 0x7541F000	(C:\Windows\System32\msvcrt.dll)

WS2_32.dll	A: 0x760E0000 - 0x76143000	(C:\Windows\System32\WS2_32.dll)

RPCRT4.dll	A: 0x75BD0000 - 0x75C8A000	(C:\Windows\System32\RPCRT4.dll)

IMM32.DLL	A: 0x75F40000 - 0x75F65000	(C:\Windows\System32\IMM32.DLL)

samp-node.DLL	A: 0x655C0000 - 0x65615000	(C:\Users\sheen\Projects\SA-MP\samp-mini-mission\plugins\samp-node.DLL)

MSVCP140.dll	A: 0x72410000 - 0x72480000	(C:\Windows\SYSTEM32\MSVCP140.dll)

node.dll	A: 0x784A0000 - 0x798D9000	(C:\Users\sheen\Projects\SA-MP\samp-mini-mission\node.dll)

VCRUNTIME140.dll	A: 0x71DC0000 - 0x71DD4000	(C:\Windows\SYSTEM32\VCRUNTIME140.dll)

ADVAPI32.dll	A: 0x75920000 - 0x75999000	(C:\Windows\System32\ADVAPI32.dll)

sechost.dll	A: 0x75A60000 - 0x75AD5000	(C:\Windows\System32\sechost.dll)

dbghelp.dll	A: 0x72480000 - 0x72608000	(C:\Windows\SYSTEM32\dbghelp.dll)

PSAPI.DLL	A: 0x75900000 - 0x75906000	(C:\Windows\System32\PSAPI.DLL)

CRYPT32.dll	A: 0x75100000 - 0x751FF000	(C:\Windows\System32\CRYPT32.dll)

bcrypt.dll	A: 0x756B0000 - 0x756C9000	(C:\Windows\System32\bcrypt.dll)

IPHLPAPI.DLL	A: 0x74680000 - 0x746B2000	(C:\Windows\SYSTEM32\IPHLPAPI.DLL)

USERENV.dll	A: 0x75010000 - 0x75035000	(C:\Windows\SYSTEM32\USERENV.dll)

powrprof.dll	A: 0x70B10000 - 0x70B54000	(C:\Windows\SYSTEM32\powrprof.dll)

UMPDC.dll	A: 0x6F590000 - 0x6F59D000	(C:\Windows\SYSTEM32\UMPDC.dll)

mswsock.dll	A: 0x74520000 - 0x74572000	(C:\Windows\system32\mswsock.dll)

kernel.appcore.dll	A: 0x74610000 - 0x7461F000	(C:\Windows\SYSTEM32\kernel.appcore.dll)

CRYPTBASE.DLL	A: 0x74FE0000 - 0x74FEA000	(C:\Windows\SYSTEM32\CRYPTBASE.DLL)

bcryptPrimitives.dll	A: 0x759A0000 - 0x759FC000	(C:\Windows\System32\bcryptPrimitives.dll)

napinsp.dll	A: 0x745D0000 - 0x745E1000	(C:\Windows\system32\napinsp.dll)

pnrpnsp.dll	A: 0x745B0000 - 0x745C6000	(C:\Windows\system32\pnrpnsp.dll)

wshbth.dll	A: 0x745A0000 - 0x745B0000	(C:\Windows\system32\wshbth.dll)

NLAapi.dll	A: 0x74580000 - 0x74596000	(C:\Windows\system32\NLAapi.dll)

DNSAPI.dll	A: 0x74480000 - 0x74511000	(C:\Windows\SYSTEM32\DNSAPI.dll)

NSI.dll	A: 0x756D0000 - 0x756D7000	(C:\Windows\System32\NSI.dll)

winrnr.dll	A: 0x74470000 - 0x7447E000	(C:\Windows\System32\winrnr.dll)

mdnsNSP.dll	A: 0x74440000 - 0x74461000	(C:\Program Files (x86)\Bonjour\mdnsNSP.dll)

rasadhlp.dll	A: 0x74430000 - 0x74438000	(C:\Windows\System32\rasadhlp.dll)

Solution (for now):

  • Put away the promise from inside of SA-MP callbacks.

Example:

OnGameModeInit(() => {
  console.log('print here');
});

sequelize.sync()
  .then(() => console.log('Connected to the database.'))
  .catch(e => console.error('An error occured while trying to connect to database.', e));
commented

Try making the OnGameModeInit handler asynchronous.

Try making the OnGameModeInit handler asynchronous.

It's still crashing

OnGameModeInit(async () => {
  console.log('print here');

  sequelize.sync()
    .then(() => console.log('Connected to the database.'))
    .catch(e => console.error('An error occured while trying to connect to database.', e));
});
commented

Try making the OnGameModeInit handler asynchronous.

It's still crashing

OnGameModeInit(async () => {

  console.log('print here');



  sequelize.sync()

    .then(() => console.log('Connected to the database.'))

    .catch(e => console.error('An error occured while trying to connect to database.', e));

});

Try writing your own basic promise instead. Not all libraries are 100% guaranteed compatible.

Try making the OnGameModeInit handler asynchronous.

It's still crashing

OnGameModeInit(async () => {

  console.log('print here');



  sequelize.sync()

    .then(() => console.log('Connected to the database.'))

    .catch(e => console.error('An error occured while trying to connect to database.', e));

});

Try writing your own basic promise instead. Not all libraries are 100% guaranteed compatible.

You're right. It works now.

const myPromise = new Promise((resolve, reject) => resolve('hi from promise.'));

OnGameModeInit(async () => {
  console.log('print here');

  myPromise.then((value) => console.log(value));
  // sequelize.sync()
  //   .then(() => console.log('Connected to the database.'))
  //   .catch(e => console.error('An error occured while trying to connect to database.', e));
});

server-log.txt

----------
Loaded log file: "server_log.txt".
----------

SA-MP Dedicated Server
----------------------
v0.3.7-R2, (C)2005-2015 SA-MP Team

language = ""  (string)

Server Plugins
--------------
 Loading plugin: samp-node        
  Loaded.
 Loaded 1 plugins.


Started server on port: 8192, with maxplayers: 5 lanmode is OFF.


Filterscripts
---------------
  Loaded 0 filterscripts.

print here
hi from promise.

It would be good to avoid promises from other library for now.

Example to use database with samp-node:

const mariadb = require('mariadb/callback');

const { OnGameModeInit, OnGameModeExit } = require('samp-node-lib');

const conn = mariadb.createConnection({ host: 'localhost', user: 'root', database: 'samp_mini_mission' });

OnGameModeInit(async () => {
  console.log('print here');

  conn.connect(err => {
    if (err) {
      console.error('Failed to connect database', err);
    } else {
      console.log('Success to connect database', conn);
    }
  });
});

OnGameModeExit(async () => {
  conn.end(err => {
    if (err) {
      console.error('Failed to end database connection', err);
    } else {
      console.log('Failed to end database connection', conn);
    }
  });
})
commented

Wahey! I'm glad you could've got it fixed pal.

I don't think you should avoid promises entirely though. It might actually be worth trying TypeORM, it is promise / async based and works in many different environments. I currently use TypeORM with my AltV server, so I have a good feeling it may work fine with samp-node.

commented

@rsetiawan7 This is going to be fixed in near future, the reason it crashes the server is because of running synchronous in samp event scopes, pawn vm expects it to be done already but node uv loop is not done yet

my plan is running it as an async context so we can achieve that easily, not to mention this will cost not being able to use returned values

image
new version

commented

fixed in #15 thanks @plutalov