Skip to content

Commit 59a592f

Browse files
committed
Add end_of_file handling to TcpBufferedSocket.
This fixes #3891. Also removed debug!(...) statement from socket destructor which causes a crash when the logging level is set to debug.
1 parent 53ec6c3 commit 59a592f

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/libstd/net_tcp.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ pub fn TcpSocket(socket_data: @TcpSocketData) -> TcpSocket {
4848
*/
4949
struct TcpSocketBuf {
5050
data: @TcpBufferedSocketData,
51+
mut end_of_stream: bool,
5152
}
5253

5354
pub fn TcpSocketBuf(data: @TcpBufferedSocketData) -> TcpSocketBuf {
5455
TcpSocketBuf {
55-
data: data
56+
data: data,
57+
end_of_stream: false
5658
}
5759
}
5860

@@ -782,6 +784,7 @@ impl TcpSocketBuf: io::Reader {
782784
let err_data = read_result.get_err();
783785

784786
if err_data.err_name == ~"EOF" {
787+
self.end_of_stream = true;
785788
break;
786789
} else {
787790
debug!("ERROR sock_buf as io::reader.read err %? %?",
@@ -808,13 +811,21 @@ impl TcpSocketBuf: io::Reader {
808811
}
809812
fn read_byte() -> int {
810813
let mut bytes = ~[0];
811-
if self.read(bytes, 1u) == 0 { fail } else { bytes[0] as int }
814+
if self.read(bytes, 1u) == 0 {
815+
if self.end_of_stream {
816+
-1
817+
} else {
818+
fail
819+
}
820+
} else {
821+
bytes[0] as int
822+
}
812823
}
813824
fn unread_byte(amt: int) {
814825
self.data.buf.unshift(amt as u8);
815826
}
816827
fn eof() -> bool {
817-
false // noop
828+
self.end_of_stream
818829
}
819830
fn seek(dist: int, seek: io::SeekStyle) {
820831
log(debug, fmt!("tcp_socket_buf seek stub %? %?", dist, seek));
@@ -871,7 +882,8 @@ fn tear_down_socket_data(socket_data: @TcpSocketData) unsafe {
871882
uv::ll::close(stream_handle_ptr, tcp_socket_dtor_close_cb);
872883
};
873884
core::comm::recv(closed_po);
874-
log(debug, fmt!("about to free socket_data at %?", socket_data));
885+
//the line below will most likely crash
886+
//log(debug, fmt!("about to free socket_data at %?", socket_data));
875887
rustrt::rust_uv_current_kernel_free(stream_handle_ptr
876888
as *libc::c_void);
877889
log(debug, ~"exiting dtor for tcp_socket");

0 commit comments

Comments
 (0)