public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Ken Brown <kbrown@cornell.edu>
To: sten.kristian.ivarsson@gmail.com
Cc: cygwin@cygwin.com
Subject: Re: AF_UNIX/SOCK_DGRAM is dropping messages
Date: Tue, 30 Mar 2021 10:17:13 -0400	[thread overview]
Message-ID: <860668bf-8cf9-0969-6a01-7fbf8b782db1@cornell.edu> (raw)
In-Reply-To: <00d901d7208e$97c05c50$c74114f0$@gmail.com>

On 3/24/2021 5:18 AM, Kristian Ivarsson via Cygwin wrote:
> Hi Glenn
> 
> Thanks for the reply, so more below
> 
>>> 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.
> 
> To send a larger buffer (in this case 16 KB) is intentional, but just the sizeof int is relevant. The reason is just to send many bytes and verify that they end up on the other side in correct order
> 
> 
>> 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.)
> 
> It was not and the updated version does not either, but that is not the issue though but I added a test to verify that the whole chunk is sent/read
> 
>> 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.
> 
> That might be the case, but this is blocking attempts though (or maybe I've misunderstood the flags ?), but regardless of that, the test-case is not about how to handle partial writes/reads though, but to kind of show that messages seems to be lost, but of cource code need not to be flawed so thanx for the feedback
> 
> It almost seems like it is UDP-semantics and that packages can get lost or end up in non sequential order, and of course SOCK_DGRAM tells you that, but the posix description says "UNIX domain datagram sockets are always reliable and don't reorder datagrams"
> 
> It seems like when an internal buffer or so of 64 KB is filled the rest of the packages are dropped until consumed, so in this case the 32 first packages arrive in correct order but after that any random package (with index > 32) seems to end up at the "server"
> 
>> 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.
> 
> That's done in the updated version, or at least verified

Thanks for the test case.  I can confirm the problem.  I'm not familiar enough 
with the current AF_UNIX implementation to debug this easily.  I'd rather spend 
my time on the new implementation (on the topic/af_unix branch).  It turns out 
that your test case fails there too, but in a completely different way, due to a 
bug in sendto for datagrams.  I'll see if I can fix that bug and then try again.

Ken

  reply	other threads:[~2021-03-30 14:18 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-23 15:37 sten.kristian.ivarsson
2021-03-23 19:20 ` Glenn Strauss
2021-03-24  9:18   ` sten.kristian.ivarsson
2021-03-30 14:17     ` Ken Brown [this message]
2021-03-31  8:24       ` sten.kristian.ivarsson
2021-03-31 15:07         ` Ken Brown
2021-04-01 16:02           ` Ken Brown
2021-04-06  7:52             ` Noel Grandin
2021-04-06 14:59               ` Ken Brown
2021-04-06 14:50             ` sten.kristian.ivarsson
2021-04-06 15:24               ` Ken Brown
2021-04-07 14:56               ` Ken Brown
2021-04-08  8:37                 ` sten.kristian.ivarsson
2021-04-08 19:47                   ` sten.kristian.ivarsson
2021-04-08 21:02                     ` Ken Brown
2021-04-13 14:06                 ` sten.kristian.ivarsson
2021-04-13 14:47                   ` Ken Brown
2021-04-13 22:43                     ` Ken Brown
2021-04-14 15:53                       ` Ken Brown
2021-04-14 17:14                       ` sten.kristian.ivarsson
2021-04-14 21:58                         ` Ken Brown
2021-04-15 13:15                           ` sten.kristian.ivarsson
2021-04-15 15:01                             ` Ken Brown
2021-04-27 14:56                               ` Ken Brown
2021-04-28  7:15                                 ` sten.kristian.ivarsson
2021-08-12 12:56                                   ` sten.kristian.ivarsson
2021-08-13 11:19                                     ` Ken Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=860668bf-8cf9-0969-6a01-7fbf8b782db1@cornell.edu \
    --to=kbrown@cornell.edu \
    --cc=cygwin@cygwin.com \
    --cc=sten.kristian.ivarsson@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).