Setting termios VMIN > 0 and VTIME > 0 on non blocking file

classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: Setting termios VMIN > 0 and VTIME > 0 on non blocking file

Cygwin list mailing list

On 2020-03-14 11:23, Åke Rehnman wrote:

>
> On 2020-03-13 11:12, Corinna Vinschen wrote:
>> On Mar 12 18:04, Åke Rehnman via Cygwin wrote:
>>> On 2020-03-12 16:08, Corinna Vinschen wrote:
>>>> On Mar 12 15:44, Corinna Vinschen wrote:
>>>>> On Mar 12 15:20, Åke Rehnman via Cygwin wrote:
>>>>>> I think the problem is if the number of bytes requested are more
>>>>>> than what
>>>>> To clarify: number of bytes == VMIN?
>>> no number of bytes requested from ReadFile(). As far as I know Win32
>>> has no
>>> concept of VMIN.
>> Right, just this weird TimeoutMultiplier, but nevertheless I wasn't sure
>> what you meant.
>>
>>>>>> is in the buffer it is going to overlap the read function
>>>>>> (because of VTIME)
>>>>>> and immediately after that CancelIO is called. Contrary to what
>>>>>> is mentioned
>>>>>> in the source code I think CancelIO is actually discarding data...
>>>>> So far we didn't have that experience.  CancelIO is usually safe
>>>>> in this regard.
>>> The data is MIA somehow...
>> Yes, but we're calling CancelIo in other circumstances in Cygwin and
>> there were no reports of missing data.  CancelIo is just supposed to
>> terminate the currently running overlapped IO, not to discard any
>> in-flight data.  If that's different for serial IO, there would be no
>> way to terminate serial overlapped IO gracefully.  Well, yeah, it's
>> Windows, but still...
> I have scrutinized the microsoft serial driver reference
> implementation
> (https://github.com/microsoft/Windows-driver-samples/blob/master/serial/serial/read.c)
> and it looks to me as CancelIo does not purge any data. However since
> the driver is vendor dependent in my case FTDI it is impossible to
> know exactly what is going on since it is closed source.
>>>>>> -    if ((vmin_ > 0) && (vtime_ == 0))
>>>>>> +       if (is_nonblocking())
>>>>>> +       {
>>>>>> +               to.ReadIntervalTimeout = MAXDWORD;
>>>>>> +       }
>>>>>> +    else if ((vmin_ > 0) && (vtime_ == 0))
>>>>> What if you switch to !O_NONBLOCK after calling tcsetattr?  The
>>>>> setting of ReadIntervalTimeout would be lost then.
>>>>>
>>>>> Either we have to repeat calling SetCommTimeouts every time
>>>>> we switch mode, or we have to do the above setting temporary
>>>>> every time we call ReadFile in non blocking mode.
>>> True.
>>>> What about this:
>>>>
>>>> diff --git a/winsup/cygwin/fhandler_serial.cc
>>>> b/winsup/cygwin/fhandler_serial.cc
>>>> --- a/winsup/cygwin/fhandler_serial.cc
>>>> +++ b/winsup/cygwin/fhandler_serial.cc
>>>> @@ -68,6 +68,16 @@ fhandler_serial::raw_read (void *ptr, size_t& ulen)
>>>>        goto err;
>>>>          else if (ev)
>>>>        termios_printf ("error detected %x", ev);
>>>> +      else if (is_nonblocking ())
>>>> +    {
>>>> +      if (!st.cbInQue)
>>>> +        {
>>>> +          tot = -1;
>>>> +          set_errno (EAGAIN);
>>>> +          goto out;
>>>> +        }
>>>> +      inq = st.cbInQue;
>>>> +    }
>>>>          else if (st.cbInQue && !vtime_)
>>>>        inq = st.cbInQue;
>>>>          else if (!is_nonblocking () && !overlapped_armed)
>>> Looks promising. I will try it.
> Your patch works (for my test case and screen). Question is if we have
> to consider the case where ulen==0 ...
>>>
>>> BTW there is a gremlin in the "else if (ev)" line....
>> A gremlin?  Would you mind to explain?  Btw., if you find a bug
>> in the code, we do take patches :) https://cygwin.com/contrib.html
> If we have an error event in ev it will make a blocking read even if
> VTIME==0.
>
I forgot, also any CancelIo should be terminated with a blocking
GetOverlappedResult() see this excellent blog post
https://devblogs.microsoft.com/oldnewthing/20110202-00/?p=11613

    // took longer than 1 second - cancel it and give up
    CancelIo(h);
    WaitForSingleObject(o.hEvent, INFINITE); // added // Alternatively:
GetOverlappedResult(h, &o, TRUE);
    return WAIT_TIMEOUT;

/Ake

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Reply | Threaded
Open this post in threaded view
|

Re: Setting termios VMIN > 0 and VTIME > 0 on non blocking file

Corinna Vinschen-2
On Mar 14 11:36, Åke Rehnman via Cygwin wrote:
> On 2020-03-14 11:23, Åke Rehnman wrote:
> > Your patch works (for my test case and screen). Question is if we have
> > to consider the case where ulen==0 ...

Thanks for testing!

> > > > BTW there is a gremlin in the "else if (ev)" line....
> > > A gremlin?  Would you mind to explain?  Btw., if you find a bug
> > > in the code, we do take patches :) https://cygwin.com/contrib.html
> > If we have an error event in ev it will make a blocking read even if
> > VTIME==0.

Ah, yeah, I was aware of that, I just ignored it for now since I'm not
sure what the best way to handle that is.

Two options come to mind, either ignoring these errors entirely, or
returning -1 with errno set to EIO, along the lines of the Linux test
for tty_io_error() at the start of tty_read.

However, I have a sinking feeling that the function needs a rewrite
anyway.

For instance, consider reading in blocking mode, which may result in
running the for loop more than once.  If the first loop successfully
read 4 bytes, and the second loop runs into an error from
ClearCommError, the function will return -1 with errno set, completely
ignoring the fact that 4 bytes have been read already.

It should return 4 in this case, and only the next run of
fhandler_serial::raw_read *might* return -1.

This code really shows its age...


> I forgot, also any CancelIo should be terminated with a blocking
> GetOverlappedResult() see this excellent blog post
> https://devblogs.microsoft.com/oldnewthing/20110202-00/?p=11613
>
>    // took longer than 1 second - cancel it and give up
>    CancelIo(h);
>    WaitForSingleObject(o.hEvent, INFINITE); // added // Alternatively:
> GetOverlappedResult(h, &o, TRUE);
>    return WAIT_TIMEOUT;

Thanks, fixed in git.


Corinna

--
Corinna Vinschen
Cygwin Maintainer

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Setting termios VMIN > 0 and VTIME > 0 on non blocking file

Corinna Vinschen-2
Hi Åke,

On Mar 16 10:35, Corinna Vinschen wrote:

> On Mar 14 11:36, Åke Rehnman via Cygwin wrote:
> > On 2020-03-14 11:23, Åke Rehnman wrote:
> > > > > BTW there is a gremlin in the "else if (ev)" line....
> > > > A gremlin?  Would you mind to explain?  Btw., if you find a bug
> > > > in the code, we do take patches :) https://cygwin.com/contrib.html
> > > If we have an error event in ev it will make a blocking read even if
> > > VTIME==0.
>
> Ah, yeah, I was aware of that, I just ignored it for now since I'm not
> sure what the best way to handle that is.
>
> Two options come to mind, either ignoring these errors entirely, or
> returning -1 with errno set to EIO, along the lines of the Linux test
> for tty_io_error() at the start of tty_read.
>
> However, I have a sinking feeling that the function needs a rewrite
> anyway.
>
> For instance, consider reading in blocking mode, which may result in
> running the for loop more than once.  If the first loop successfully
> read 4 bytes, and the second loop runs into an error from
> ClearCommError, the function will return -1 with errno set, completely
> ignoring the fact that 4 bytes have been read already.
>
> It should return 4 in this case, and only the next run of
> fhandler_serial::raw_read *might* return -1.
>
> This code really shows its age...
I have revamped the fhandler_serial::raw_read function, but I need
somebody for testing.  Any chance you volunteer for that job?
We may need a couple of days to iron out all problems, but I think
the code is more sound now than the original function.  I even
added a couple of comments to the code (*gasp*) so there's stuff
to discuss and to see why I did it that way.

For a quicker turn around we can also use Freenode IRC, see
https://cygwin.com/irc.html

Interested?


Thanks,
Corinna

--
Corinna Vinschen
Cygwin Maintainer

--
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Setting termios VMIN > 0 and VTIME > 0 on non blocking file

Cygwin list mailing list

On 2020-03-17 13:16, Corinna Vinschen wrote:

> Hi Åke,
>
> On Mar 16 10:35, Corinna Vinschen wrote:
>> On Mar 14 11:36, Åke Rehnman via Cygwin wrote:
>>> On 2020-03-14 11:23, Åke Rehnman wrote:
>>>>>> BTW there is a gremlin in the "else if (ev)" line....
>>>>> A gremlin?  Would you mind to explain?  Btw., if you find a bug
>>>>> in the code, we do take patches :) https://cygwin.com/contrib.html
>>>> If we have an error event in ev it will make a blocking read even if
>>>> VTIME==0.
>> Ah, yeah, I was aware of that, I just ignored it for now since I'm not
>> sure what the best way to handle that is.
>>
>> Two options come to mind, either ignoring these errors entirely, or
>> returning -1 with errno set to EIO, along the lines of the Linux test
>> for tty_io_error() at the start of tty_read.
>>
>> However, I have a sinking feeling that the function needs a rewrite
>> anyway.
>>
>> For instance, consider reading in blocking mode, which may result in
>> running the for loop more than once.  If the first loop successfully
>> read 4 bytes, and the second loop runs into an error from
>> ClearCommError, the function will return -1 with errno set, completely
>> ignoring the fact that 4 bytes have been read already.
>>
>> It should return 4 in this case, and only the next run of
>> fhandler_serial::raw_read *might* return -1.
>>
>> This code really shows its age...
> I have revamped the fhandler_serial::raw_read function, but I need
> somebody for testing.  Any chance you volunteer for that job?
> We may need a couple of days to iron out all problems, but I think
> the code is more sound now than the original function.  I even
> added a couple of comments to the code (*gasp*) so there's stuff
> to discuss and to see why I did it that way.
>
> For a quicker turn around we can also use Freenode IRC, see
> https://cygwin.com/irc.html
>
> Interested?

Sure, bring it on!

/Ake


--
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple
12