From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 115022 invoked by alias); 1 Nov 2018 20:56:31 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 115008 invoked by uid 89); 1 Nov 2018 20:56:31 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.3.2 spammy=H*F:D*gov, CYGWIN, UD:nih.gov, Lavrentiev X-HELO: nihcesxway2.hub.nih.gov Received: from nihcesxway2.hub.nih.gov (HELO nihcesxway2.hub.nih.gov) (128.231.90.124) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 01 Nov 2018 20:56:29 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nih.gov; i=@nih.gov; q=dns/txt; s=NIH; t=1541105790; x=1572641790; h=from:to:subject:date:message-id: content-transfer-encoding:mime-version; bh=H4G5nj1OAty8bSzvNK1TQxn9mqALjLSWvGnOKD4X9FU=; b=Jz+OZTzB4QMh+W+DKq03pky9rpdWFsON6m4TMr+vRfizFgjT+0W303Kp 5D1N6ZTbSIDqk2VUzzYxlMkVFceB/3cn8JkCz3quWG4bAmcH2bJrv91Ib 1fCd2z66uw6e0lc+9mC19FxkKa3bTosHXJ0tvpgnfAWtVmqaXn7VpOQl1 mbj/g7Hvcm+uJ8KM7YkY6LODPkNj/QYct3ljkht2iGdvHaTNX0PSgU/g+ SOZxBLBI2T4z3b0vEbSStcPTc1bxntBm2qOxwOJ3QPpHO0cQQ3KxmLvcb tG6K+49ZlyjwG1HixnCeYrfXvxRTDAy+56/7SAvrnmUYNZYCN9XgAqD61 w==; Received: from uccbx03.nih.gov (HELO ces.nih.gov) ([156.40.79.153]) by nihcesxway2.hub.nih.gov with ESMTP; 01 Nov 2018 16:56:24 -0400 Received: from uccbx04.nih.gov (156.40.79.154) by uccbX03.nih.gov (156.40.79.153) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 1 Nov 2018 16:56:23 -0400 Received: from GCC01-CY1-obe.outbound.protection.outlook.com (156.40.79.132) by uccbx04.nih.gov (156.40.79.154) with Microsoft SMTP Server (TLS) id 15.0.1367.3 via Frontend Transport; Thu, 1 Nov 2018 16:56:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nih.onmicrosoft.com; s=selector1-ncbi-nlm-nih-gov; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=H4G5nj1OAty8bSzvNK1TQxn9mqALjLSWvGnOKD4X9FU=; b=Eoz+++hhilwjNUjQkHg+ZFVH3YDLSiQ8IZMGnhuSsaYpiY65SPMaylnETIFxI0bKfXN9/gCVOcmaewixleYn0N+1+FRRfG0iS9sOg8W2qIVUt73Ae1jNskaLyuHY2FtXGg2Q1Zg3Lo/3gKeGDcorUPHHM1zrZf/ygnFgFEw67/U= Received: from SN6PR09MB3247.namprd09.prod.outlook.com (20.177.250.225) by SN6PR09MB3245.namprd09.prod.outlook.com (20.177.250.223) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1273.27; Thu, 1 Nov 2018 20:56:22 +0000 Received: from SN6PR09MB3247.namprd09.prod.outlook.com ([fe80::4d0a:8286:ea4c:3885]) by SN6PR09MB3247.namprd09.prod.outlook.com ([fe80::4d0a:8286:ea4c:3885%5]) with mapi id 15.20.1250.020; Thu, 1 Nov 2018 20:56:22 +0000 From: "Lavrentiev, Anton (NIH/NLM/NCBI) [C] via cygwin" Reply-To: "Lavrentiev, Anton (NIH/NLM/NCBI) [C]" To: "'cygwin@cygwin.com'" Subject: SOCK_NONBLOCK not honored Date: Thu, 01 Nov 2018 20:56:00 -0000 Message-ID: authentication-results: spf=none (sender IP is ) smtp.mailfrom=lavr@ncbi.nlm.nih.gov; Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Return-Path: lavr@ncbi.nlm.nih.gov X-SW-Source: 2018-11/txt/msg00017.txt.bz2 Hi, Looks like CYGWIN defines but does not honor the SOCK_NONBLOCK flag when us= ed with socket(2). (It also defines SOCK_CLOEXEC but I haven't checked whether it is honored -= - full disclosure.) Consider the following code: $ cat bug.c #include #include #include #include #include #include #include #include #ifdef BUG # define SOCK_TYPE (SOCK_STREAM | SOCK_NONBLOCK) #else # define SOCK_TYPE SOCK_STREAM #endif int main() { char buf[80]; ssize_t n; struct sockaddr_in in; int s =3D socket(AF_INET, SOCK_TYPE, 0); #ifndef BUG fcntl(s, F_SETFL, O_NONBLOCK); #endif memset(&in, 0, sizeof(in)); in.sin_family =3D AF_INET; in.sin_addr.s_addr =3D htonl(INADDR_LOOPBACK); in.sin_port =3D htons(6666); if (connect(s, (struct sockaddr*) &in, sizeof(in)) < 0) { int err; socklen_t len =3D sizeof(err); struct pollfd pfd; if (errno !=3D EINPROGRESS) { printf("connect failed immediately: %m\n"); return 1; } memset(&pfd, 0, sizeof(pfd)); pfd.fd =3D s; pfd.events =3D POLLOUT; if (!poll(&pfd, 1, 1000)) { printf("poll timed-out\n"); return 2; } if (pfd.revents & (POLLERR | POLLHUP)) { printf("poll failed 0x%04X\n", pfd.revents); return 3; } if (getsockopt(s, SOL_SOCKET, SO_ERROR, (void*) &err, &len) !=3D 0)= { printf("getsockopt failed: %m\n"); return 4; } if (err !=3D 0) { printf("connect failed: %s\n", strerror(err)); return 5; } } errno =3D 0; n =3D recv(s, buf, sizeof(buf), 0); printf("Got %zd byte(s): %m\n", n); return 0; } When the program is built on Linux with and without the macro BUG defined, = the result is all the same, the last recv() correctly finishes with -1 and EAGAIN (run "nc -l 6666" as = a server). $ gcc -Wall -o bug bug.c $ ./bug Got -1 byte(s): Resource temporarily unavailable $ gcc -Wall -DBUG -o bug bug.c $ ./bug Got -1 byte(s): Resource temporarily unavailable However, on CYGWIN when the code is compiled with -DBUG, the last recv() be= comes blocking, and the program never exits: $ gcc -Wall -o bug bug.c $ ./bug Got -1 byte(s): Resource temporarily unavailable $ gcc -Wall -DBUG -o bug bug.c $ ./bug ... strace confirms that the recv() has blocked the execution. Also, connect()= appears to be blocking, too, but it's harder to see. Thanks, Anton Lavrentiev -- 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