From efd2defba0cccbe153737376a02cc354a3cb7fca Mon Sep 17 00:00:00 2001 From: Andreia Gaita Date: Thu, 28 Feb 2013 21:54:45 +0000 Subject: [PATCH] Set timeouts on the socket so we don't hang forever if something bad happens. Don't set keepalive if it's not requested. --- .../Mono.Debugger.Soft/Connection.cs | 18 ++++++++++++++++-- .../VirtualMachineManager.cs | 4 ++++ src/Debugger/Backend.Sdb/VirtualMachine.cs | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/external/Mono.Debugger.Soft/Mono.Debugger.Soft/Connection.cs b/external/Mono.Debugger.Soft/Mono.Debugger.Soft/Connection.cs index 9849902..652efb9 100644 --- a/external/Mono.Debugger.Soft/Mono.Debugger.Soft/Connection.cs +++ b/external/Mono.Debugger.Soft/Mono.Debugger.Soft/Connection.cs @@ -1049,6 +1049,8 @@ namespace Mono.Debugger.Soft while (offset < len) { int n = TransportReceive (buf, buf_offset + offset, len - offset); + if (n < 0) + return n; if (n == 0) return offset; offset += n; @@ -1105,6 +1107,8 @@ namespace Mono.Debugger.Soft byte[] header = new byte [HEADER_LENGTH]; int len = Receive (header, 0, header.Length); + if (len < 0) + return null; if (len == 0) return new byte [0]; if (len != HEADER_LENGTH) { @@ -1173,6 +1177,9 @@ namespace Mono.Debugger.Soft bool ReceivePacket () { byte[] packet = ReadPacket (); + if (packet == null) + return true; + if (packet.Length == 0) { return false; } @@ -1635,7 +1642,8 @@ namespace Mono.Debugger.Soft internal void SetSocketTimeouts (int send_timeout, int receive_timeout, int keepalive_interval) { TransportSetTimeouts (send_timeout, receive_timeout); - SendReceive (CommandSet.VM, (int)CmdVM.SET_KEEPALIVE, new PacketWriter ().WriteId (keepalive_interval)); + if (keepalive_interval > 0) + SendReceive (CommandSet.VM, (int)CmdVM.SET_KEEPALIVE, new PacketWriter ().WriteId (keepalive_interval)); } internal long[] VM_GetTypesForSourceFile (string fname, bool ignoreCase) { @@ -2340,7 +2348,13 @@ namespace Mono.Debugger.Soft protected override int TransportReceive (byte[] buf, int buf_offset, int len) { - return socket.Receive (buf, buf_offset, len, SocketFlags.None); + try { + return socket.Receive (buf, buf_offset, len, SocketFlags.None); + } catch (SocketException ex) { + if (ex.ErrorCode == (int)SocketError.TimedOut) + return -1; + throw; + } } protected override void TransportSetTimeouts (int send_timeout, int receive_timeout) diff --git a/external/Mono.Debugger.Soft/Mono.Debugger.Soft/VirtualMachineManager.cs b/external/Mono.Debugger.Soft/Mono.Debugger.Soft/VirtualMachineManager.cs index a5bdf4f..145b9a7 100644 --- a/external/Mono.Debugger.Soft/Mono.Debugger.Soft/VirtualMachineManager.cs +++ b/external/Mono.Debugger.Soft/Mono.Debugger.Soft/VirtualMachineManager.cs @@ -35,6 +35,8 @@ namespace Mono.Debugger.Soft private delegate VirtualMachine ListenCallback (Socket dbg_sock, Socket con_sock); private delegate VirtualMachine ConnectCallback (Socket dbg_sock, Socket con_sock, IPEndPoint dbg_ep, IPEndPoint con_ep); + public static int DefaultTimeout { get; set; } + internal VirtualMachineManager () { } @@ -328,6 +330,8 @@ namespace Mono.Debugger.Soft vm.StandardOutput = standardOutput; vm.StandardError = standardError; + if (DefaultTimeout > 0) + vm.SetSocketTimeouts (DefaultTimeout, DefaultTimeout, 0); transport.EventHandler = new EventHandler (vm); diff --git a/src/Debugger/Backend.Sdb/VirtualMachine.cs b/src/Debugger/Backend.Sdb/VirtualMachine.cs index 14c69ca..1cb2638 100644 --- a/src/Debugger/Backend.Sdb/VirtualMachine.cs +++ b/src/Debugger/Backend.Sdb/VirtualMachine.cs @@ -97,6 +97,7 @@ namespace Debugger.Backend.Sdb public void Attach (int port) { LogProvider.Log ("Attempting connection at port {0}...", port); + MDS.VirtualMachineManager.DefaultTimeout = 500; vm = MDS.VirtualMachineManager.Connect (new IPEndPoint (IPAddress.Loopback, port)); vm.EnableEvents ( MDS.EventType.AppDomainCreate,