Skip to content

Commit

Permalink
add a closed state to prevent double close
Browse files Browse the repository at this point in the history
  • Loading branch information
saviorand committed Dec 29, 2024
1 parent ff0b3a7 commit a7ff98f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lightbug_http/server.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,17 @@ struct Server:
max_request_body_size = default_max_request_body_size

var req_number = 0
var is_closed = False

while True:
req_number += 1

b = Bytes(capacity=default_buffer_size)
bytes_recv = conn.read(b)
if bytes_recv == 0:
conn.close()
if not is_closed:
conn.close()
is_closed = True
break

var request = HTTPRequest.from_bytes(self.address(), max_request_body_size, b^)
Expand All @@ -182,8 +185,10 @@ struct Server:
try:
res = handler.func(request)
except:
_ = conn.write(encode(InternalError()))
conn.close()
if not is_closed:
_ = conn.write(encode(InternalError()))
conn.close()
is_closed = True
return

var close_connection = (not self.tcp_keep_alive) or request.connection_close()
Expand All @@ -194,4 +199,8 @@ struct Server:
var written = conn.write(encode(res^))

if close_connection or written == -1:
conn.close()
if not is_closed:
conn.close()
is_closed = True
break

0 comments on commit a7ff98f

Please sign in to comment.