Skip to content

Commit

Permalink
protos/tcp: respect async operations order
Browse files Browse the repository at this point in the history
In case there is a write request, before actually writing the data on
the network, check if there are any async chunks left, and make sure
they are sent before any other write happens. This is necessary to make
sure the order of the packets is respected, otherwise we might end up
with fragments of other packets in the stream.

Thanks go to Vlad Patrascu for reporting and testing this.
  • Loading branch information
razvancrainea committed Jan 11, 2018
1 parent 769fb3c commit 5773e65
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/proto_bin/proto_bin.c
Expand Up @@ -491,6 +491,12 @@ inline static int _bin_write_on_socket(struct tcp_connection *c, int fd,

lock_get(&c->write_lock);
if (bin_async) {
/*
* if there is any data pending to write, we have to wait for those chunks
* to be sent, otherwise we will completely break the messages' order
*/
if (((struct bin_data*)c->proto_data)->async_chunks_no)
return add_write_chunk(c, buf, len, 0);
n=async_tsend_stream(c,fd,buf,len, bin_async_local_write_timeout);
} else {
n = tsend_stream(fd, buf, len, bin_send_timeout);
Expand Down
6 changes: 6 additions & 0 deletions modules/proto_hep/proto_hep.c
Expand Up @@ -641,6 +641,12 @@ inline static int _hep_write_on_socket(struct tcp_connection *c, int fd,

lock_get(&c->write_lock);
if (hep_async) {
/*
* if there is any data pending to write, we have to wait for those chunks
* to be sent, otherwise we will completely break the messages' order
*/
if (((struct hep_data*)c->proto_data)->async_chunks_no)
return add_write_chunk(c, buf, len, 0);
n=async_tsend_stream(c,fd,buf,len, hep_async_local_write_timeout);
} else {
n = tsend_stream(fd, buf, len, hep_send_timeout);
Expand Down
6 changes: 6 additions & 0 deletions net/proto_tcp/proto_tcp.c
Expand Up @@ -728,6 +728,12 @@ inline static int _tcp_write_on_socket(struct tcp_connection *c, int fd,

lock_get(&c->write_lock);
if (tcp_async) {
/*
* if there is any data pending to write, we have to wait for those chunks
* to be sent, otherwise we will completely break the messages' order
*/
if (((struct tcp_data*)c->proto_data)->async_chunks_no)
return add_write_chunk(c, buf, len, 0);
n=async_tsend_stream(c,fd,buf,len,tcp_async_local_write_timeout);
} else {
n=tsend_stream(fd, buf, len, tcp_send_timeout);
Expand Down

0 comments on commit 5773e65

Please sign in to comment.