Record several node. js errors and Solutions

Source: Internet
Author: User
Tags emit

Record several node. js errors and Solutions

Several node. js errors and solutions are as follows:

Node. js Error: EBADF, write

I recently wrote a small project for my work. I thought I could have had a good tea, but I was surprised to hear that an error occurred.

Fs. js: 77
Throw err; // Forgot a callback but don't know where? Use NODE_DEBUG = fs
^
Error: EBADF, write
At Error (native)

WTF ?! Fs. js: 77? This is why I want to see the kernel source code? Fortunately, the project is not big, and the demolition is healthier. It took about half a day to figure it out.

The first is the Error name, EBADF, which indicates the bad file descriptor Error file descriptor.
But Error: EBADF, write indicates to write data to the wrong file descriptor.

In short, there is a BUG. on ('data') events get data written into fd. At this time, an operation throws an error and I close this fd when processing the error, the other side is still triggering the data event to write data to this (my close) fd. As follows:

//...
 
Var fd = fs. openSync (path, 'w ');
 
Test. on ('data', function (data ){
Fs. write (fd, data );
});
 
Test. on ('end', function (){
Fs. close (fd );
});
 
// The Error: EBADF, write will occur when you close the end.
SetTimeout (function (){
Fs. close (fd );
}, 10 );
 
//...

Solution: Check the location where fs. close closes the file descriptor to ensure that no read/write operations will be performed after close.


Error: EBADF, close

In addition, I found another similar error in Google's process. This is when you do fs for a variety of situations. close (fd);, but unfortunately, multiple cases are triggered, fs. if you call close (fd) multiple times, an EBADF error will also occur. In this way, you can see:

Test. on ('end', function (){
Fs. close (fd );
Fs. close (fd); // when multiple calls are made
});

Unfriendly Error Reporting


Fs. js: 77
Throw err; // Forgot a callback but don't know where? Use NODE_DEBUG = fs
^
Error: EBADF, close
At Error (native)

Solution: Check fs. close again, but this time it is necessary to ensure that multiple processes will not execute fs. close repeatedly, or you can ignore it using try/catch.

Error: EBADF, bad file descriptor

Finally, when the fd fails and the read operation is performed, I thought there would be an Error: EBADF, And the read result does not exist. The following code attempts to create a BUG:


//...
 
Fs. closeSync (fd );
Fs. readSync (fd, new Buffer (1024), 0, 1024 );
 
//...

However, this error will be much more friendly, with the call stack opened.

Fs. js: 552
Var r = binding. read (fd, buffer, offset, length, position );
^
Error: EBADF, bad file descriptor
At Error (native)
At Object. fs. readSync (fs. js: 552: 19)
At command. <anonymous> (/Users/Lellansin/Documents/workspace/node/test-server/app/services/TestService. js: 40: 6)
At command. emit (events. js: 110: 17)
At ChildProcess. emit (events. js: 129: 20)
At maybeClose (child_process.js: 1015: 16)
At Socket. <anonymous> (child_process.js: 1183: 11)
At Socket. emit (events. js: 107: 17)
At Pipe. close (net. js: 485: 12)

Solution: Check the error stack to modify the code ..


Node. js Error: stdout maxBuffer exceeded

When using the exec, execFile, spawnSync, execFileSync, and execSync methods in the child_process module, pay attention to the maxBuffer items in the options parameter.

During execution, the above method creates a buffer in the memory to buffer the combination of all output data, while maxBuffer specifies the buffer size. If the output exceeds the specified size, the maxBuffer exceeded error is returned.

The solution is to estimate the size during execution and set a larger maxBuffer:

Var exec = require('child_process'cmd.exe c;
 
Var child = exec ('LS-loan ',{
Encoding: 'utf8 ',
Timeout: 0,
MaxBuffer: 5000*1024, // The default value is 200*1024.
KillSignal: 'signature'
}, Function (err, stdout, stderr ){
Console. log (stdout );
});

Or, when the spawn. on ('data') event is triggered, You can manually splice data to the. on ('close') event to obtain complete data.


Pomelo Cannot call method 'forwardmessage' of undefined

Error message:
[14:32:45. 315] [DEBUG] pomelo-[E: \ svn \ xjmh \ trunk \ src \ server \ game-server \ node_modules \ pomelo \ lib \ components \
Connector. js] [connector-server-1] handleMessage session id: 1, msg: {"id": 10, "type": 0, "compressRoute": 0, "route ": "user. u
SerHandler. login "," body ":" {username: 'Alan _ 1', password: '000000', dev_id: '000000 '}"}
[14:32:45. 320] [ERROR] pomelo-[E: \ svn \ xjmh \ trunk \ src \ server \ game-server \ node_modules \ pomelo \ lib \ server \ serv
Er. js] fail to forward message: TypeError: Cannot call method 'forwardmessage' of undefined
At doForward (E: \ svn \ xjmh \ trunk \ src \ server \ game-server \ node_modules \ pomelo \ lib \ server. js: 334: 50)
At dispatch (E: \ svn \ xjmh \ trunk \ src \ server \ game-server \ node_modules \ pomelo \ lib \ server. js: 103: 7)
At next (E: \ svn \ xjmh \ trunk \ src \ server \ game-server \ node_modules \ pomelo \ lib \ common \ service \ filterService. js: 50: 7)
At Service. beforeFilter (E: \ svn \ xjmh \ trunk \ src \ server \ game-server \ node_modules \ pomelo \ lib \ common \ service \ filterServi
Ce. js: 65: 3)
At beforeFilter (E: \ svn \ xjmh \ trunk \ src \ server \ game-server \ node_modules \ pomelo \ lib \ server. js: 242: 8)
At pro. globalHandle (E: \ svn \ xjmh \ trunk \ src \ server \ game-server \ node_modules \ pomelo \ lib \ server. js: 112: 3)
At Component. globalHandle (E: \ svn \ xjmh \ trunk \ src \ server \ game-server \ node_modules \ pomelo \ lib \ components \ server. js: 74:
14)
At handleMessage (E :\ svn \ xjmh \ trunk \ src \ server \ game-server \ node_modules \ pomelo \ lib \ components \ connector. js: 295: 15)
At null. <anonymous> (E: \ svn \ xjmh \ trunk \ src \ server \ game-server \ node_modules \ pomelo \ lib \ components \ connector. js: 239: 5)
 
At EventEmitter. emit (events. js: 95: 17)

In Pomelo Club, rpc fails because the front-end server fails to forward messages to other servers. Or it may be handler spelling error.
The blogger checks and finds that in servers. json, this problem occurs if the common server is configured with "frontend": true. It is normal to try to remove frontend or change it to false.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.