Skip to content

Commit 0e2842e

Browse files
committed
[SERVER] Make shared vars atomic, handle threadpool submission failure
1 parent ef52031 commit 0e2842e

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

src/server/globals.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ struct _dnbd3_uplink
103103
atomic_bool shutdown; // signal this thread to stop, must only be set from uplink_shutdown() or cleanup in uplink_mainloop()
104104
bool replicatedLastBlock; // bool telling if the last block has been replicated yet
105105
bool cycleDetected; // connection cycle between proxies detected for current remote server
106-
int nextReplicationIndex; // Which index in the cache map we should start looking for incomplete blocks at
106+
atomic_int nextReplicationIndex; // Which index in the cache map we should start looking for incomplete blocks at
107107
// If BGR == BGR_HASHBLOCK, -1 means "currently no incomplete block"
108108
atomic_uint_fast64_t bytesReceived; // Number of bytes received by the uplink since startup.
109109
atomic_uint_fast64_t bytesReceivedLastSave; // Number of bytes received when we last saved the cache map
110110
int queueLen; // length of queue (slots; either BGR (no client at all) or one or more clients)
111-
int idleTime; // How many seconds the uplink was idle (apart from keep-alives)
111+
atomic_int idleTime; // How many seconds the uplink was idle (apart from keep-alives)
112112
dnbd3_queue_entry_t *queue;
113113
atomic_uint_fast32_t queueId;
114114
dnbd3_alt_local_t altData[SERVER_MAX_ALTS];

src/server/uplink.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ static void switchToNewServer(dnbd3_uplink_t *uplink)
854854
*/
855855
static int sendReplicationRequest(dnbd3_uplink_t *uplink)
856856
{
857-
assert_uplink_thread();
857+
assert_uplink_thread(); // Make sure we're not called from any other thread
858858
if ( uplink->current.fd == -1 )
859859
return -1; // Should never be called in this state, consider send error
860860
if ( _backgroundReplication == BGR_DISABLED || uplink->cacheFd == -1 )
@@ -1079,8 +1079,6 @@ static void handleReceive(dnbd3_uplink_t *uplink)
10791079
uplink->image->problem.queue = false;
10801080
}
10811081
mutex_unlock( &uplink->queueLock );
1082-
// We don't remove the entry from the list here yet, to slightly increase the chance of other
1083-
// clients attaching to this request while we write the data to disk
10841082
if ( entry->to - entry->from != inReply.size ) {
10851083
logadd( LOG_WARNING, "Received payload length does not match! (is: %"PRIu32", expect: %u, %s:%d)",
10861084
inReply.size, (unsigned int)( entry->to - entry->from ), PIMG(uplink->image) );
@@ -1092,12 +1090,16 @@ static void handleReceive(dnbd3_uplink_t *uplink)
10921090
}
10931091
if ( likely( uplink->cacheFd != -1 ) ) {
10941092
tparams.jobs++;
1095-
threadpool_run( &handleReceiveSaveToDisk, &tparams, NULL );
1093+
if ( !threadpool_run( &handleReceiveSaveToDisk, &tparams, NULL ) ) {
1094+
handleReceiveSaveToDisk( &tparams );
1095+
}
10961096
}
10971097
// 2) Send to any waiting clients
10981098
if ( entry->clients != NULL ) {
10991099
tparams.jobs++;
1100-
threadpool_run( &handleReceiveSendToClients, &tparams, NULL );
1100+
if ( !threadpool_run( &handleReceiveSendToClients, &tparams, NULL ) ) {
1101+
handleReceiveSendToClients( &tparams );
1102+
}
11011103
}
11021104
// 3) Trigger more background replication if applicable
11031105
if ( sendReplicationRequest( uplink ) == -1 ) {

0 commit comments

Comments
 (0)