Skip to content

Commit

Permalink
Use process.kill to fix the process.exit hang
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Mar 21, 2024
1 parent d774644 commit eb72e24
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 24 deletions.
3 changes: 3 additions & 0 deletions typescript/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ vs vsc:

test:
make -C examples/hello

lint:
./node_modules/.bin/eslint --fix r2pipe/*.ts
4 changes: 2 additions & 2 deletions typescript/r2pipe/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export abstract class R2PipeBase implements R2PipeCmdInterface {
abstract cmd(command: string): Promise<string>;
abstract quit(): Promise<boolean>;

async cmdj(command: string): Promise<any> {
async cmdj(command: string): Promise<object> {
const output = await this.cmd(command);
try {
return JSON.parse(output);
Expand All @@ -25,7 +25,7 @@ export interface R2PipeCmdInterface {
* @param command The radare2 command to execute.
* @returns A promise that resolves with the parsed JSON output.
*/
cmdj(command: string): Promise<any>;
cmdj(command: string): Promise<object>;

/**
* Quits and destroys the given instance
Expand Down
6 changes: 3 additions & 3 deletions typescript/r2pipe/http.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as http from "http";
import {R2PipeBase, R2PipeCmdInterface} from "./base.js";
import {R2PipeBase} from "./base.js";

export class R2PipeHttp extends R2PipeBase {
private baseUrl: string;
Expand All @@ -20,7 +20,7 @@ export class R2PipeHttp extends R2PipeBase {
return new Promise((resolve, reject) => {
const url = `${uri}/cmd/${cmd}`;
// console.error("==> " + url);
http.get(url, (res: any) => {
http.get(url, (res: http.IncomingMessage) => {
if (res.statusCode !== 200) {
reject(new Error(`Request Failed. Status Code: ${res.statusCode}`));
res.resume(); // Consume response data to free up memory
Expand All @@ -32,7 +32,7 @@ export class R2PipeHttp extends R2PipeBase {
res.on('end', () => {
resolve(rawData);
});
}).on('error', (e: any) => {
}).on('error', (e: Error) => {
reject(new Error(`Error making HTTP request: ${e.message}`));
});
});
Expand Down
2 changes: 1 addition & 1 deletion typescript/r2pipe/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {R2PipeBase, R2PipeCmdInterface} from "./base.js";
import {R2PipeCmdInterface} from "./base.js";
import {R2PipeHttp} from "./http.js";
import {R2PipeSpawn} from "./spawn.js";
import {R2PipeLocal} from "./local.js";
Expand Down
1 change: 1 addition & 0 deletions typescript/r2pipe/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class R2PipeLocal extends R2PipeBase {
async quit(): Promise<boolean> {
this.stream.dispose();
this.stream = null;
process.kill(process.pid, 'SIGINT');
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions typescript/r2pipe/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface R2PipeQueueItem {
cmd: string;
result: string;
cb: any;
error: any;
error: Error;
}

export class R2PipeQueue {
Expand All @@ -24,7 +24,7 @@ export class R2PipeQueue {
this.input.destroy();
this.output.destroy();
}
cmd(cmd: string, cb: Function) {
cmd(cmd: string, cb: any) {
this.pipeQueue.push({
cmd: cmd,
cb: cb,
Expand Down
23 changes: 7 additions & 16 deletions typescript/r2pipe/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export class R2PipeSpawn extends R2PipeBase {
async cmd(command: string): Promise<string> {
return new Promise((resolve, reject) => {
this.r2cb.cmd(command, (error, res) => {
if (error) {
reject(error);
} else {
resolve(res);
}
if (error) {
reject(error);
} else {
resolve(res);
}
});
});
//return this.httpCmd(this.filePath, command);
Expand Down Expand Up @@ -49,7 +49,7 @@ function pipeCmd(proc, cmd, cb) {
}

function pipeCmdOutput(proc, data, cb) {
var len = data.length;
let len = data.length;

if (this.pipeQueue.length < 1) {
return cb(new Error('r2pipe error: No pending commands for incomming data'));
Expand Down Expand Up @@ -96,15 +96,6 @@ function r2bind(child, cb, r2cmd) {
// addr must be a NativePointer not a number
this.cmd(s + "@0x" + Number(addr).toString(16), cb2);
},
plugin: function (s, cb2) {
//throw new Exception("not implemented");
},
unload: function (s, cb2) {
// throw new Exception("not implemented");
},
log: function (msg) {
console.log(msg);
},
/* Run cmd and return plaintext output */
cmd: function (s, cb2) {
pipeCmd.bind(r2)(child, s, cb2);
Expand Down Expand Up @@ -172,7 +163,7 @@ function r2bind(child, cb, r2cmd) {

child.on('close', function (code, signal) {
running = false;
let error = errmsg ? errmsg : '';
const error = errmsg ? errmsg : '';
if (signal) {
cb(new Error('Child received signal ' + signal + '\n' + error));
} else {
Expand Down

0 comments on commit eb72e24

Please sign in to comment.