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 E52AB68D5 for ; Wed, 6 Jul 2011 07:21:16 +0000 (UTC) Received: (qmail 19851 invoked by uid 500); 6 Jul 2011 07:21:13 -0000 Delivered-To: apmail-apr-dev-archive@apr.apache.org Received: (qmail 19431 invoked by uid 500); 6 Jul 2011 07:21:05 -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 19410 invoked by uid 99); 6 Jul 2011 07:20:58 -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 07:20:58 +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 [74.125.83.178] (HELO mail-pv0-f178.google.com) (74.125.83.178) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 06 Jul 2011 07:20:51 +0000 Received: by pvg7 with SMTP id 7so7845297pvg.37 for ; Wed, 06 Jul 2011 00:20:30 -0700 (PDT) MIME-Version: 1.0 Received: by 10.142.249.42 with SMTP id w42mr3925428wfh.195.1309936828720; Wed, 06 Jul 2011 00:20:28 -0700 (PDT) Received: by 10.142.139.6 with HTTP; Wed, 6 Jul 2011 00:20:28 -0700 (PDT) X-Originating-IP: [213.164.164.164] In-Reply-To: References: Date: Wed, 6 Jul 2011 09:20:28 +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 Hi all again, I've noticed high CPU usage, and the fact is that there's a problem with accept and socket timeouts and my code. I'll review http://dev.ariel-networks.com/apr/apr-tutorial/html/apr-tutoria= l-13.html#ss13.4 and will post a correct code later on, just for the records. Cheers, Antonio 2011/7/6 Antonio Vieiro : > 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) { > =A0do { > =A0 =A0status =3D apr_socket_accept(&client_socket, server_socket, server= _pool); > =A0} while (!server_stop && APR_STATUS_IS_EAGAIN(status)); > } > > And the signal handler just sets the "server_stop" flag to some non-zero = value. > > 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 = list of >>> functions which can be safely run from a signal handler is very short, = and >>> 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.) >> >