diff --git a/runtime/rpc_server.hpp b/runtime/rpc_server.hpp index c59340b..98f6c9d 100644 --- a/runtime/rpc_server.hpp +++ b/runtime/rpc_server.hpp @@ -124,12 +124,14 @@ public: auto err = ::bind(socket, (struct sockaddr*)&address, sizeof(address)); if (err < 0) { + close(socket); return Err { .msg = std::format("could not bind ({})", err) }; }; } { auto err = ::listen(socket, 0); if (err < 0) { + close(socket); return Err { .msg = std::format("could not listen ({})", err) }; } } @@ -140,6 +142,7 @@ public: int client = ::accept( socket, (struct sockaddr*)&client_address, &address_size); if (client < 0) { + close(socket); return Err { .msg = std::format("could not accept ({})", client) }; } @@ -149,10 +152,7 @@ public: std::string message = {}; while (true) { ssize_t bytes_read = read(client, buffer, buf_len); - if (bytes_read < 0) { - return Err { .msg - = std::format("could not read ({})", bytes_read) }; - } else if (bytes_read == 0) { + if (bytes_read <= 0) { break; } for (size_t i = 0; i < (size_t)bytes_read; ++i) { @@ -168,6 +168,7 @@ public: "error parsing rpc message: '{}' @ {}:{}\n", err.msg, err.pos.line, err.pos.col); close(client); + close(socket); return Err { .msg = msg, };