Skip to content

Commit

Permalink
mosh-server: Fix bug where spins forever if process dies while discon…
Browse files Browse the repository at this point in the history
…nected
  • Loading branch information
keithw committed Nov 26, 2012
1 parent 6a16eec commit 486325e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
39 changes: 16 additions & 23 deletions src/frontend/mosh-server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network

const int timeout_if_no_client = 60000;
int timeout = min( network.wait_time(), terminal.wait_time( now ) );
if ( !network.get_remote_state_num() ) {
if ( (!network.get_remote_state_num())
|| network.shutdown_in_progress() ) {
timeout = min( timeout, timeout_if_no_client );
}

Expand All @@ -542,7 +543,9 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
assert( fd_list.size() == 1 ); /* servers don't hop */
int network_fd = fd_list.back();
sel.add_fd( network_fd );
sel.add_fd( host_fd );
if ( !network.shutdown_in_progress() ) {
sel.add_fd( host_fd );
}

int active_fds = sel.select( timeout );
if ( active_fds < 0 ) {
Expand Down Expand Up @@ -618,7 +621,7 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
}
}

if ( sel.read( host_fd ) ) {
if ( (!network.shutdown_in_progress()) && sel.read( host_fd ) ) {
/* input from the host needs to be fed to the terminal */
const int buf_size = 16384;
char buf[ buf_size ];
Expand All @@ -630,23 +633,17 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
EIO (see #264). So we treat errors on read() like EOF. */
if ( bytes_read <= 0 ) {
bytes_read = 0;
if ( !network.has_remote_addr() ) {
spin(); /* let 60-second timer take care of this */
} else if ( !network.shutdown_in_progress() ) {
network.start_shutdown();
}
}

string terminal_to_host = terminal.act( string( buf, bytes_read ) );
network.start_shutdown();
} else {
string terminal_to_host = terminal.act( string( buf, bytes_read ) );

/* update client with new state of terminal */
if ( !network.shutdown_in_progress() ) {
/* update client with new state of terminal */
network.set_current_state( terminal );
}

/* write any writeback octets back to the host */
if ( swrite( host_fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) {
break;
/* write any writeback octets back to the host */
if ( swrite( host_fd, terminal_to_host.c_str(), terminal_to_host.length() ) < 0 ) {
break;
}
}
}

Expand All @@ -664,13 +661,9 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network
break;
}

if ( sel.error( host_fd ) ) {
if ( (!network.shutdown_in_progress()) && sel.error( host_fd ) ) {
/* host problem */
if ( network.has_remote_addr() ) {
network.start_shutdown();
} else {
spin(); /* let 60-second timer take care of this */
}
network.start_shutdown();
}

/* quit if our shutdown has been acknowledged */
Expand Down
11 changes: 10 additions & 1 deletion src/network/transportsender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ TransportSender<MyState>::TransportSender( Connection *s_connection, MyState &in
verbose( false ),
shutdown_in_progress( false ),
shutdown_tries( 0 ),
shutdown_start( -1 ),
ack_num( 0 ),
pending_data_ack( false ),
SEND_MINDELAY( 8 ),
Expand Down Expand Up @@ -357,7 +358,15 @@ void TransportSender<MyState>::process_acknowledgment_through( uint64_t ack_num
template <class MyState>
bool TransportSender<MyState>::shutdown_ack_timed_out( void ) const
{
return shutdown_tries >= SHUTDOWN_RETRIES;
if ( shutdown_in_progress ) {
if ( shutdown_tries >= SHUTDOWN_RETRIES ) {
return true;
} else if ( timestamp() - shutdown_start >= uint64_t( ACTIVE_RETRY_TIMEOUT ) ) {
return true;
}
}

return false;
}

/* Executed upon entry to new receiver state */
Expand Down
3 changes: 2 additions & 1 deletion src/network/transportsender.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ namespace Network {
bool verbose;
bool shutdown_in_progress;
int shutdown_tries;
uint64_t shutdown_start;

/* information about receiver state */
uint64_t ack_num;
Expand Down Expand Up @@ -132,7 +133,7 @@ namespace Network {
void remote_heard( uint64_t ts ) { last_heard = ts; }

/* Starts shutdown sequence */
void start_shutdown( void ) { shutdown_in_progress = true; }
void start_shutdown( void ) { if ( !shutdown_in_progress ) { shutdown_start = timestamp(); shutdown_in_progress = true; } }

/* Misc. getters and setters */
/* Cannot modify current_state while shutdown in progress */
Expand Down

0 comments on commit 486325e

Please sign in to comment.