Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f719ca7

Browse files
committed
fix: close the socket if connecting failed
1 parent 645bee4 commit f719ca7

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ setInterval(() => {
175175

176176
[CONTRIBUTING.md](./CONTRIBUTING.md)
177177

178+
## Development
179+
180+
1. Setup rust and Node.js.
181+
2. Modify the code.
182+
2. `npm run build && npm run test`
183+
178184
## LICENSE
179185

180186
MIT

__test__/seqpacket.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ if (!kIsDarwin) {
361361

362362
expect(() => client.connect(kServerpath)).toThrow();
363363

364+
// should close the socket if connecting failed
365+
expect(client._state()).toBe(5);
366+
367+
client.destroy();
368+
// it should be okay to destroy multiple times
364369
client.destroy();
365370
});
366371

js/addon.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export function socketClose(fd: number): void
88
export class SeqpacketSocketWrap {
99
constructor(ee: object, fd?: number | undefined | null)
1010
init(thisObj: object): void
11+
state(): number
1112
close(): void
1213
shutdownWrite(): void
1314
uvRefer(): void

js/seqpacket.ts

+8
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,12 @@ export class SeqpacketSocket extends EventEmitter {
290290
this.destroyed = true;
291291
this.wrap.close();
292292
}
293+
294+
/**
295+
* For test only
296+
* @ignore
297+
*/
298+
_state() {
299+
return this.wrap.state()
300+
}
293301
}

src/seqpacket.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::os::raw::c_int;
44

55
use crate::socket::{self, get_loop, sockaddr_from_string, Emitter, HandleData, UvRefence};
66
use crate::util::{
7-
addr_to_string, buf_into_vec, error, get_err, resolve_libc_err, resolve_uv_err,
8-
set_clo_exec, set_non_block, socket_addr_to_string, uv_err_msg,
7+
addr_to_string, buf_into_vec, error, get_err, resolve_libc_err, resolve_uv_err, set_clo_exec,
8+
set_non_block, socket_addr_to_string, uv_err_msg,
99
};
1010
use libc::{sockaddr, sockaddr_un, EAGAIN, EINTR, EINVAL, ENOBUFS, EWOULDBLOCK};
1111
use napi::{Env, JsBuffer, JsFunction, JsNumber, JsObject, JsString, JsUnknown, Ref, Result};
@@ -120,6 +120,11 @@ impl SeqpacketSocketWrap {
120120
Ok(())
121121
}
122122

123+
#[napi]
124+
pub fn state(&self) -> i32 {
125+
self.state as i32
126+
}
127+
123128
#[napi]
124129
pub fn close(&mut self) -> Result<()> {
125130
if self.state == State::Closed {
@@ -534,15 +539,13 @@ impl SeqpacketSocketWrap {
534539

535540
let err = errno();
536541

537-
if ret == -1 && err != 0 {
538-
if err == libc::EINPROGRESS {
539-
// not an error
540-
} else if err == libc::ECONNRESET || err == EINVAL {
542+
// libc::EINPROGRESS is not an error
543+
if ret == -1 && err != 0 && err != libc::EINPROGRESS {
544+
if err == libc::ECONNRESET || err == EINVAL {
541545
// TODO should we delay error?
542-
resolve_libc_err(ret)?;
543-
} else {
544-
resolve_libc_err(ret)?;
545546
}
547+
self.close()?;
548+
resolve_libc_err(ret)?;
546549
}
547550

548551
unsafe {
@@ -600,11 +603,13 @@ impl SeqpacketSocketWrap {
600603
}
601604

602605
extern "C" fn on_close(handle: *mut sys::uv_handle_t) {
603-
unsafe { assert!(!(*handle).data.is_null(), "unexpected null handle data"); };
606+
unsafe {
607+
assert!(!(*handle).data.is_null(), "unexpected null handle data");
608+
};
604609
unsafe {
605610
let mut data = Box::from_raw((*handle).data as *mut HandleData);
606611
data.unref().unwrap();
607-
Box::from_raw(handle);
612+
let _ = Box::from_raw(handle);
608613
};
609614
}
610615

@@ -615,7 +620,9 @@ macro_rules! on_event {
615620
return;
616621
}
617622

618-
unsafe { assert!(!(*handle).data.is_null(), "unexpected null handle data"); };
623+
unsafe {
624+
assert!(!(*handle).data.is_null(), "unexpected null handle data");
625+
};
619626
let data = unsafe { Box::from_raw((*handle).data as *mut HandleData) };
620627
let wrap = data.inner_mut_ref::<&mut SeqpacketSocketWrap>().unwrap();
621628
wrap.$fn(status, events);

0 commit comments

Comments
 (0)