AF_UNIX/SOCK_DGRAM is dropping messages

Glenn Strauss gs-cygwin.com@gluelogic.com
Tue Mar 23 19:20:28 GMT 2021


On Tue, Mar 23, 2021 at 04:37:52PM +0100, Kristian Ivarsson via Cygwin wrote:
> Hi all
> 
> Using AF_UNIX/SOCK_DGRAM with current version (3.2.0) seems to drop messages or at least they are not received in the same order they are sent
> 
> Attached C:ish (with C++ threads though) sample program that essentially creates a "client" that writes as much as possible and a "server" that consumes as much as possible
> 
> It seems like some buffer is filled and then messages are dropped (or at least it appears so) (if someone is about to test this, the "sleep" might need to be adjusted in order to make this happen)
> 
> Hopefully it's just a flaw in our application (and sample), but as far as we can see, this should work
> 
> 
> Does anyone, perhaps named Ken, have any insightful thoughts about this ?


> const int size = BUFSIZ * 2;


>     char buffer[size] = {};
> 
>     for( int idx = 0; idx < count; ++idx)
>     {
>         memcpy( buffer, &idx, sizeof idx);
> 
>         const ssize_t result = sendto( fd, buffer, size, 0, (struct sockaddr*)&address, sizeof address);


>             const ssize_t result = recv( fd, buffer, size, 0);
...
>             int index = 0;
>             memcpy( &index, buffer, sizeof idx);

This appears to be a programming error, unrelated to Cygwin.

I know that what you provided was an example test case, but you might
want to check if your app is sending way too much when the actual
payload size is much smaller.  In the example you provided, you are
sending 16KB instead of 4 bytes for the count.

Is your code handling partial read/recv and partial write/sendto?
(It is definitely a bug in the use of recv() in the sample code.)

Partial reads and writes can occur more frequently with non-blocking
sockets, but it is still good defensive programming to detect and
handle partial read/writes.

It goes without saying that if your protocol sends a fixed size chunk
of data, that you should ensure that you read the entire fixed size,
even if only using part of the data.

Cheers, Glenn


More information about the Cygwin mailing list