From 9f1239c56ea8f0e284dea84fc1a66df2582288b8 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 17 Dec 2022 19:08:59 +0200 Subject: [PATCH] vweb,net: fix vweb crash when the connection was closed prematurely (tested with Chrome on windows, refreshing as fast as possible) --- vlib/net/tcp.v | 3 +++ vlib/vweb/vweb.v | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/vlib/net/tcp.v b/vlib/net/tcp.v index 0bc97f1961..788fc87f1e 100644 --- a/vlib/net/tcp.v +++ b/vlib/net/tcp.v @@ -151,6 +151,9 @@ pub fn (mut c TcpConn) read_deadline() !time.Time { // write_ptr blocks and attempts to write all data pub fn (mut c TcpConn) write_ptr(b &u8, len int) !int { + $if trace_tcp_sock_handle ? { + eprintln('>>> TcpConn.write_ptr | c: ${ptr_str(c)} | c.sock.handle: ${c.sock.handle} | b: ${ptr_str(b)} | len: ${len}') + } $if trace_tcp ? { eprintln( '>>> TcpConn.write_ptr | c.sock.handle: ${c.sock.handle} | b: ${ptr_str(b)} len: ${len} |\n' + diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 66feb024b5..ebeca9b3b3 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -717,10 +717,16 @@ pub fn not_found() Result { } fn send_string(mut conn net.TcpConn, s string) ! { + $if trace_send_string_conn ? { + eprintln('> send_string: conn: ${ptr_str(conn)}') + } $if trace_response ? { eprintln('> send_string:\n${s}\n') } - conn.write(s.bytes())! + if voidptr(conn) == unsafe { nil } { + return error('connection was closed before send_string') + } + conn.write_string(s)! } // Do not delete.