From dev-return-24293-apmail-apr-dev-archive=apr.apache.org@apr.apache.org Wed Jul 6 06:33:39 2011 Return-Path: X-Original-To: apmail-apr-dev-archive@www.apache.org Delivered-To: apmail-apr-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B74B16DCC for ; Wed, 6 Jul 2011 06:33:39 +0000 (UTC) Received: (qmail 70176 invoked by uid 500); 6 Jul 2011 06:33:38 -0000 Delivered-To: apmail-apr-dev-archive@apr.apache.org Received: (qmail 69642 invoked by uid 500); 6 Jul 2011 06:33:19 -0000 Mailing-List: contact dev-help@apr.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Id: Delivered-To: mailing list dev@apr.apache.org Received: (qmail 69042 invoked by uid 99); 6 Jul 2011 06:33:07 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 06 Jul 2011 06:33:07 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [209.85.210.50] (HELO mail-pz0-f50.google.com) (209.85.210.50) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 06 Jul 2011 06:32:58 +0000 Received: by pzk2 with SMTP id 2so5101282pzk.37 for ; Tue, 05 Jul 2011 23:32:37 -0700 (PDT) MIME-Version: 1.0 Received: by 10.142.120.1 with SMTP id s1mr3881667wfc.252.1309933957112; Tue, 05 Jul 2011 23:32:37 -0700 (PDT) Received: by 10.142.139.6 with HTTP; Tue, 5 Jul 2011 23:32:37 -0700 (PDT) X-Originating-IP: [213.164.164.164] In-Reply-To: References: Date: Wed, 6 Jul 2011 08:32:37 +0200 Message-ID: Subject: Re: Graceful server shutdown? From: Antonio Vieiro To: dev@apr.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org Thanks guys, that's the approach I am following. The accept loop looks something like: apr_socket_opt_set(server_socket, APR_SO_NONBLOCK, 1); apr_socket_timeout_set(server_socket, 100); ... while (!server_stop) { do { status =3D apr_socket_accept(&client_socket, server_socket, server_pool= ); } while (!server_stop && APR_STATUS_IS_EAGAIN(status)); } And the signal handler just sets the "server_stop" flag to some non-zero va= lue. This works fine on a Windows box, I'll try to see how well it behaves on a Unix box later on, and will post here just for the record. Thanks, Antonio 2011/7/5 Jeff Trawick : > On Tue, Jul 5, 2011 at 11:00 AM, Wes Garland wrote: >>> On both cases I imagine the server cleanup (apr_pool_destroy) should >>> be performed after the server loop, and not in the signal handler, >>> right? >> >> Correct. Your signal handler should just set a flag and return.=A0 The l= ist of >> functions which can be safely run from a signal handler is very short, a= nd >> malloc/free are not among them. >> >>> b) Shall I set a "keep-running" flag (using mutexes, i.e, a >>> synchronized flag) for shutdown on the SIGINT handler? >> >> Not mutexes.=A0 There is no need and the pthread mutex functions (which = APR is >> implemented on top of on UNIX) are not async-signal safe.=A0 Here is my = basic >> pattern: >> >> int die =3D 0; >> int signalHandler(signal) >> { >> =A0 die =3D signal; >> } >> >> while (!die) >> { >> =A0 /* accept, fork */ >> } > > Another piece of the puzzle is that APR on Unix can return EINTR > (signal interruption) from a few critical functions > (apr_socket_accept() and apr_poll*() and maybe something else), but > generally eats EINTR internally. This has an impact on when your main > thread gets to check the die flag. > > This is all rather non-portable. =A0(You can get WSAEINTR from > apr_socket_accept() on Windows but that means something totally > different.) >