From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id 3F7F5387701E for ; Sat, 14 Mar 2020 10:36:48 +0000 (GMT) Received: by mail-lf1-x12c.google.com with SMTP id b186so9868634lfg.11 for ; Sat, 14 Mar 2020 03:36:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:references:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=tmZrkPzlStfIflJLZlRwF79+4zt7c96G0jBwpQkOq/g=; b=sSrE18ab2mABikYgFlCW8dHw/HZV1/qp7ZpDDcPnTABUbVKoB+MDlOJXwu11VwygSc 1sSgN9drIONeBm3IuLlRbZMqEmJa6at4uHvoSL9u0dxXn6axylbTK2/lnoCp3eBoR69+ ZgdBC8XOH4BtzWD+oAqAsvV1ygsFbMnibcevRdbpfFr8y0EMwUmJ1SpA2DBP2Jhcg7W6 WLFvRHEm0vQkSnQQFxiJI0SnM7XrBQQApokBiXAJ66i01vchiSQmIvXMOvIqIuXKrJIh qcKYufvBMV9XllIlv/8BOLGhXKpLoO/viZvGVkcjDuUCwdKMOO/Bne4HqZszXBd2Cfcn 0mtA== X-Gm-Message-State: ANhLgQ3d+9uIhZdhayDlVR3fT8PgrOHg5XePnF17v59B4/rW4SIgp9xF BUB+JdwCbRhqx445efvvYwl3N6q+WzQ= X-Google-Smtp-Source: ADFU+vt0pNGqeZeGnIUcJ8EUCxQ8YF5+bGRdCqE6fLcikrkgZHgnWcaulmh7q3eLW38SokHyZrnumw== X-Received: by 2002:a19:760c:: with SMTP id c12mr11095022lff.48.1584182206482; Sat, 14 Mar 2020 03:36:46 -0700 (PDT) Received: from [192.168.1.117] (tb213-185-244-58.cust.teknikbyran.com. [213.185.244.58]) by smtp.googlemail.com with ESMTPSA id x62sm18380428lff.58.2020.03.14.03.36.45 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 14 Mar 2020 03:36:45 -0700 (PDT) Subject: Re: Setting termios VMIN > 0 and VTIME > 0 on non blocking file From: =?UTF-8?Q?=c3=85ke_Rehnman?= To: cygwin@cygwin.com References: <9e4c6428-59de-df07-9e33-44ba95d5497f@gmail.com> <20200312114041.GJ4042@calimero.vinschen.de> <365dd437-7553-eb4e-3253-aba3bab74895@gmail.com> <20200312144445.GP4042@calimero.vinschen.de> <20200312150833.GQ4042@calimero.vinschen.de> <9e56e005-660b-373e-d557-9c8bc97c45ba@gmail.com> <20200313101247.GD512788@calimero.vinschen.de> <2156fb6d-cba9-b70e-f8c0-7043ff52ffc3@gmail.com> Message-ID: <8e0973e2-9554-3e69-fa88-ed51fad5b191@gmail.com> Date: Sat, 14 Mar 2020 11:36:39 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <2156fb6d-cba9-b70e-f8c0-7043ff52ffc3@gmail.com> Content-Language: en-US X-Spam-Status: No, score=-17.7 required=5.0 tests=DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GARBLED_BODY, GIT_PATCH_0, GIT_PATCH_2, GIT_PATCH_3, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: cygwin@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 14 Mar 2020 10:36:49 -0000 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