Skip to content

Commit

Permalink
Fix a file transfer bug introduced in commit 2073d02
Browse files Browse the repository at this point in the history
This fixes a bug where file transfers would break when the sendto()
system call failed with errno 11 (usually indicating a full packet
buffer).

The max loops constant has also been greatly reduced, as it was
excessively CPU intensive, and most connections would not benefit
from such a high value. In the future it will be set dynamically.
  • Loading branch information
JFreegman committed Feb 15, 2022
1 parent 96843fb commit 1f6edc6
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,13 @@ static bool do_all_filetransfers(Messenger *m, int32_t friendnumber, void *userd
continue;
}

if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(
m->fr_c, friendcon->friendcon_id))) {
LOGGER_DEBUG(m->log, "Maximum connection speed reached");
// connection doesn't support any more data
return false;
}

// If the file transfer is complete, we request a chunk of size 0.
if (ft->status == FILESTATUS_FINISHED && friend_received_packet(m, friendnumber, ft->last_packet_number) == 0) {
if (m->file_reqchunk) {
Expand Down Expand Up @@ -1558,14 +1565,6 @@ static bool do_all_filetransfers(Messenger *m, int32_t friendnumber, void *userd
// The allocated slot is no longer free.
--*free_slots;
}

// Must be last to allow finishing file transfers
if (max_speed_reached(m->net_crypto, friend_connection_crypt_connection_id(
m->fr_c, friendcon->friendcon_id))) {
LOGGER_TRACE(m->log, "Maximum connection speed reached");
// connection doesn't support any more data
return false;
}
}

return true;
Expand Down Expand Up @@ -1596,7 +1595,9 @@ static void do_reqchunk_filecb(Messenger *m, int32_t friendnumber, void *userdat
// that the file transfer has finished and may end up in an infinite loop.
//
// Request up to that number of chunks per file from the client
const uint32_t max_ft_loops = 4096;
//
// TODO(Jfreegman): set this cap dynamically
const uint32_t max_ft_loops = 128;

for (uint32_t i = 0; i < max_ft_loops; ++i) {
if (!do_all_filetransfers(m, friendnumber, userdata, &free_slots)) {
Expand Down

0 comments on commit 1f6edc6

Please sign in to comment.