Skip to content

Commit c3e28bc

Browse files
committed
added deferred closing of streams after async connect() fails
1 parent 8d78fb1 commit c3e28bc

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

src/core/ev.c

+19-2
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp to) {
15021502
state = state->_next;
15031503
}
15041504
}
1505+
/* Close the stream if requested and no more listeners are left */
1506+
if ((stream->flags & JANET_STREAM_TOCLOSE) && !stream->state) {
1507+
janet_stream_close(stream);
1508+
}
15051509
}
15061510
}
15071511
}
@@ -1656,6 +1660,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
16561660
janet_unlisten(state, 0);
16571661
state = next_state;
16581662
}
1663+
/* Close the stream if requested and no more listeners are left */
1664+
if ((stream->flags & JANET_STREAM_TOCLOSE) && !stream->state) {
1665+
janet_stream_close(stream);
1666+
}
16591667
}
16601668
}
16611669
}
@@ -1854,6 +1862,10 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
18541862

18551863
state = next_state;
18561864
}
1865+
/* Close the stream if requested and no more listeners are left */
1866+
if ((stream->flags & JANET_STREAM_TOCLOSE) && !stream->state) {
1867+
janet_stream_close(stream);
1868+
}
18571869
}
18581870
}
18591871
}
@@ -1970,6 +1982,11 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
19701982
status3 == JANET_ASYNC_STATUS_DONE ||
19711983
status4 == JANET_ASYNC_STATUS_DONE)
19721984
janet_unlisten(state, 0);
1985+
/* Close the stream if requested and no more listeners are left */
1986+
JanetStream *stream = state->stream;
1987+
if ((stream->flags & JANET_STREAM_TOCLOSE) && !stream->state) {
1988+
janet_stream_close(stream);
1989+
}
19731990
}
19741991
}
19751992

@@ -2495,11 +2512,11 @@ static JanetAsyncStatus handle_connect(JanetListenerState *s) {
24952512
if (res == 0) {
24962513
janet_schedule(s->fiber, janet_wrap_abstract(s->stream));
24972514
} else {
2498-
// TODO help needed. janet_stream_close(s->stream);
2515+
s->stream->flags |= JANET_STREAM_TOCLOSE;
24992516
janet_cancel(s->fiber, janet_cstringv(strerror(res)));
25002517
}
25012518
} else {
2502-
// TODO help needed. janet_stream_close(s->stream);
2519+
s->stream->flags |= JANET_STREAM_TOCLOSE;
25032520
janet_cancel(s->fiber, janet_ev_lasterr());
25042521
}
25052522
return JANET_ASYNC_STATUS_DONE;

src/core/net.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ JANET_CORE_FN(cfun_net_connect,
480480
/* Wrap socket in abstract type JanetStream */
481481
JanetStream *stream = make_stream(sock, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE);
482482

483-
/* Set the socket to non-blocking mode */
483+
/* Set up the socket for non-blocking IO before connecting */
484484
janet_net_socknoblock(sock);
485485

486486
/* Connect to socket */

src/include/janet.h

+1
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ typedef void *JanetAbstract;
568568
#define JANET_STREAM_WRITABLE 0x400
569569
#define JANET_STREAM_ACCEPTABLE 0x800
570570
#define JANET_STREAM_UDPSERVER 0x1000
571+
#define JANET_STREAM_TOCLOSE 0x10000
571572

572573
typedef enum {
573574
JANET_ASYNC_EVENT_INIT,

0 commit comments

Comments
 (0)