Comments (5)
This is caused by sync-rpc's attempts to do cleanup. If you attache the process.on('exit'
handler before you require('sync-request')
it works fine:
console.log('Process started');
process.on('exit', () => {
try {
let res = syncRequest('GET', 'https://jsonplaceholder.typicode.com/users/1');
console.log(JSON.parse(res.getBody()));
} catch(err) {
console.log('Got error');
console.log(err);
}
});
const syncRequest = require('sync-request');
throw new Error('Boo');
If you can find a way to make this work regardless of ordering, I'd accept a PR to http://github.com/ForbesLindesay/sync-rpc. It would probably need to change https://github.com/ForbesLindesay/sync-rpc/blob/f500876453721b37376419ad855388aae0579ccf/lib/index.js#L38-L40 to give other process.on handlers a chance to run first.
from sync-request.
@ForbesLindesay, thanks for the response. Your solution works, also another way can be to put the exit event listener using process.prependListener('exit', ()=>{})
, so it's called before the sync-rpc's exit listener. I guess this is simple enough so no fix is needed.
Also there is similar issue with SIGINT
signal(replacing exit
with SIGINT
in the reproducer). In the 4.1.0 the child process never seem to receive the SIGINT(tested by putting SIGINT signal listener), but in the latest one the child process receives it and kills the request midway. Any workaround for that?
Tested on linux to make sure the separate nc
process is created.
from sync-request.
Tested little bit more for SIGINT
, for below script(hitting Ctrl+c when 'Process started' is printed):
const syncRequest = require('sync-request'); // v6.1.0
console.log('Process started');
process.on('SIGINT', () => {
console.log('got SIGINT');
try {
let res = syncRequest('GET', 'https://jsonplaceholder.typicode.com/users/1');
console.log(JSON.parse(res.getBody()));
} catch(err) {
console.log('Got error');
console.log(err);
}
});
setTimeout(() => {
console.log('timeout boo');
}, 6000);
In windows above works till node version 8.11.4, and from 8.12.0 it stops working.
Interestingly on linux it's not working for any version.
So is it expected to not work?
Error is similar to exit:
got SIGINT
Got error
Error: nodeNC failed:
events.js:183
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:50678
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
at sendMessage (C:\work\winston-test\node_modules\sync-rpc\lib\index.js:146:13)
at C:\work\winston-test\node_modules\sync-rpc\lib\index.js:175:25
at request (C:\work\winston-test\node_modules\sync-request\lib\index.js:28:15)
at process.on (C:\work\winston-test\sync-request-test.js:75:15)
at emitOne (events.js:116:13)
at process.emit (events.js:211:7)
at Signal.wrap.onsignal (internal/process.js:197:44)
timeout boo
from sync-request.
I think any kind of execution during exit is going to be a special case for sync-rpc. The problem is that one of the cleanup tasks we must do is killing the child process we spawn to handle the requests. There's no clean way I can think of for you to indicate that you have other cleanup tasks that must complete first.
from sync-request.
That makes sense, thanks @ForbesLindesay, feel free to close the issue.
from sync-request.
Related Issues (20)
- sync-rpc breaking Node 6 HOT 1
- Doesn't work with NW.js HOT 1
- How to make "timeout" take effect HOT 1
- An in-range update of cross-env is breaking the build 🚨 HOT 1
- `proxy` option doesn't work
- Possibly leaked handle? HOT 2
- Error thrown when changing HTTP method to HEAD instead of GET HOT 2
- how can this be 'sync' if it returns a promise? HOT 1
- Request headers are not honored on the redirections followed from the original resource.
- HTTP Bearer token authorisation missing
- sync-request import blocking in worker_threads
- Error: read ECONNRESET
- Doesn't work when used in VS Code plugin HOT 2
- `sync-request` leaves orphaned `sync-rpc` processes
- Check out await-sync HOT 2
- Compliance issue in sync-request HOT 2
- Error: self signed certificate HOT 1
- on nodejs 20.10.0, Uncaught Error Error: connect ECONNREFUSED 127.0.0.1:49792
- Regarding the packaging error of using esbuild
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sync-request.