From a34f98f579c1e4a758b90b3a00912a4e2c5087d6 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Sun, 23 Aug 2020 13:52:58 -0400 Subject: [PATCH] fix: Improve module_listen error handling --- runtime/src/module.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/src/module.c b/runtime/src/module.c index 8d7a7aa..5078c75 100644 --- a/runtime/src/module.c +++ b/runtime/src/module.c @@ -33,8 +33,6 @@ module_listen(struct module *module) int socket_descriptor = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); if (socket_descriptor < 0) goto err_create_socket; - module->socket_descriptor = socket_descriptor; - /* Configure the socket to allow multiple sockets to bind to the same host and port */ int optval = 1; @@ -45,6 +43,7 @@ module_listen(struct module *module) if (rc < 0) goto err_set_socket_option; /* Bind name [all addresses]:[module->port] to socket */ + module->socket_descriptor = socket_descriptor; module->socket_address.sin_family = AF_INET; module->socket_address.sin_addr.s_addr = htonl(INADDR_ANY); module->socket_address.sin_port = htons((unsigned short)module->port); @@ -70,8 +69,9 @@ done: err_add_to_epoll: err_listen: err_bind_socket: -err_set_socket_option: module->socket_descriptor = -1; +err_set_socket_option: + close(socket_descriptor); err_create_socket: err: debuglog("Socket Error: %s", strerror(errno));