From 3a434affc8589434d20ce37c3789aa4c3d3358af Mon Sep 17 00:00:00 2001 From: Geoff Gustafson Date: Fri, 8 Sep 2017 10:37:12 -0700 Subject: [PATCH] [net] Mark socket as "closing" between EOF and close event Then check for this closing flag to know that write will fail and not bother attempting it. Fixes #1512 (the follow-on report of failure on A101 w/ Ethernet) Signed-off-by: Geoff Gustafson --- src/zjs_net.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/zjs_net.c b/src/zjs_net.c index f337d9980..01b04985b 100644 --- a/src/zjs_net.c +++ b/src/zjs_net.c @@ -141,6 +141,7 @@ typedef struct sock_handle { u8_t paused; u8_t *rbuf; u8_t timer_started; + u8_t closing; u8_t closed; } sock_handle_t; @@ -511,6 +512,7 @@ static void tcp_received(struct net_context *context, DBG_PRINT("socket=%p\n", (void *)handle->socket); // NOTE: we're not really releasing anything but release_close will // just ignore the 0 args, so we can reuse the function + handle->closing = 1; zjs_defer_emit_event(handle->socket, "close", NULL, 0, NULL, release_close); } @@ -587,7 +589,7 @@ static ZJS_DECL_FUNC(socket_write) GET_SOCK_HANDLE_JS(this, handle); // FIXME: these other error cases should maybe call "error" event too - if (handle->closed) { + if (handle->closing || handle->closed) { ERR_PRINT("socket already closed\n"); return jerry_create_boolean(false); } @@ -628,7 +630,7 @@ static ZJS_DECL_FUNC(socket_write) error_desc_t desc = create_error_desc(ERROR_WRITE_SOCKET, this, function_obj); zjs_defer_emit_event(handle->socket, "error", &desc, sizeof(desc), - handle_error_arg, release_close); + handle_error_arg, zjs_release_args); return jerry_create_boolean(false); }