Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #96854

Re: Shutting down a cross-platform multithreaded app

Path csiph.com!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'see.': 0.07; 'socket': 0.07; 'cc:addr:python-list': 0.09; 'closed.': 0.09; 'feasible.': 0.09; 'sockets': 0.09; 'spawn': 0.09; 'subject:skip:m 10': 0.09; 'threads.': 0.09; 'yeah,': 0.09; 'thread': 0.10; '(at': 0.13; 'stack': 0.13; '"shutdown': 0.16; "(it's": 0.16; '_do_': 0.16; 'bind': 0.16; 'describing': 0.16; 'down).': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'packets.': 0.16; 'pipes': 0.16; 'program?': 0.16; 'rogue': 0.16; 'skip:n 50': 0.16; 'socket"': 0.16; 'socket.': 0.16; 'sure.': 0.16; 'tcp': 0.16; 'thread.': 0.16; 'threads': 0.16; 'udp,': 0.16; 'wrote:': 0.16; "wouldn't": 0.16; 'basically': 0.18; 'cheap': 0.18; 'instance,': 0.18; "shouldn't": 0.18; 'stick': 0.18; 'thoughts': 0.18; 'why.': 0.18; '>>>': 0.20; '(not': 0.20; 'windows': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'machine': 0.21; 'mind.': 0.22; 'priorities': 0.22; 'sends': 0.22; 'sep': 0.22; 'wrote': 0.23; 'represents': 0.23; 'sat,': 0.23; 'somewhere': 0.24; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'required.': 0.26; 'raw': 0.27; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; 'allocated': 0.27; 'have,': 0.27; 'initial': 0.28; 'does,': 0.29; 'firewall': 0.29; 'fork': 0.29; 'termination': 0.29; 'unix,': 0.29; "i'm": 0.30; 'connection': 0.30; 'that.': 0.30; 'becomes': 0.30; 'certainly': 0.30; 'connections': 0.30; 'agreed': 0.31; 'probably': 0.31; 'possibly': 0.32; 'source': 0.33; 'common': 0.33; 'open': 0.33; "i'll": 0.33; 'that,': 0.34; 'gives': 0.35; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'possible': 0.36; '(and': 0.36; 'assigned': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'thought': 0.37; 'seem': 0.37; 'monitor': 0.38; 'wrong': 0.38; 'anything': 0.38; 'end': 0.39; 'sure': 0.39; 'subject:-': 0.39; 'still': 0.40; 'some': 0.40; 'software': 0.40; 'ever': 0.60; 'save': 0.60; 'your': 0.60; "you'll": 0.61; 'total': 0.62; 'per': 0.62; 'skip:n 10': 0.62; 'making': 0.62; 'linked': 0.63; 'more.': 0.63; 'between': 0.65; 'else.': 0.66; 'flood': 0.66; 'here': 0.66; 'connection,': 0.72; 'sole': 0.76; 'chrisa': 0.84; 'distinguish': 0.84; 'scarce': 0.84; 'spawned': 0.84; 'subject:down': 0.84; 'understood.': 0.84; 'workers,': 0.84; 'to:none': 0.91; 'processes,': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=2x921Bsa+6scYELKPrWTK7nif51ICIu3hJ7b2KAs7hg=; b=h/Brk6tzrM9XpIFsqb9+yftGxe+nsxkO9wB+HhnYp9uYsTeMgsQ64K7cyrjd2c41sL FNJR9uYvDPzA6TGrAJp43AfnE7rFPZosIrx0+MEHjpNTFLHqdgF+HR8wx+toD4FMjGm1 lvSnS81OFugq3WmRMXVniCcBD5CbFU0Se6+F4FKG9v8866M9E5OKD/BGQ+nDMzlbdnNb qq20dk6x2kO3JD+N4jn1Eut6loAI/mZVFPp95uT2ZohCP94JtJYY75iR0TX2kA/8m8Um IdKwc++fbm8kV97etAd/ub8kbWch12ogB5dTJQLS9lXyXygho/ZrRynmfbdb0QFaDnKF Ka/A==
MIME-Version 1.0
X-Received by 10.107.132.144 with SMTP id o16mr18138169ioi.31.1442660372063; Sat, 19 Sep 2015 03:59:32 -0700 (PDT)
In-Reply-To <mtjedo$6p4$1@dont-email.me>
References <mthgrk$uh1$1@dont-email.me> <mailman.8.1442612439.21674.python-list@python.org> <mtjb0g$r5u$1@dont-email.me> <mailman.13.1442657702.21674.python-list@python.org> <mtjedo$6p4$1@dont-email.me>
Date Sat, 19 Sep 2015 20:59:31 +1000
Subject Re: Shutting down a cross-platform multithreaded app
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.20.1442660375.21674.python-list@python.org> (permalink)
Lines 87
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1442660375 news.xs4all.nl 23757 [2001:888:2000:d::a6]:47326
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:96854

Show key headers only | View raw


On Sat, Sep 19, 2015 at 8:48 PM, James Harris <james.harris.1@gmail.com> wrote:
> "Chris Angelico" <rosuav@gmail.com> wrote in message
> news:mailman.13.1442657702.21674.python-list@python.org...
>>
>> On Sat, Sep 19, 2015 at 7:49 PM, James Harris <james.harris.1@gmail.com>
>> wrote:
>>>
>>> "Chris Angelico" <rosuav@gmail.com> wrote in message
>>> news:mailman.8.1442612439.21674.python-list@python.org...
>>>> If you're using select() to monitor the sockets, you don't actually
>>>> then have to _do_ anything with the shutdown socket. You could have a
>>>> single socket that sends the shutdown signal to all your workers.
>>>
>>>
>>>
>>> I don't understand how a single socket could send the signal to all the
>>> workers. I did consider some form of multicast but thought it too
>>> complicated (and possibly infeasible).
>>
>>
>> The way I'm describing it, the workers never actually read from the
>> socket. Once that socket becomes readable, they immediately shut down,
>> without making the socket no-longer-readable.
>
>
> Understood. Good idea. Initial thoughts on it: Would work for threads. Would
> save on the number of sockets required. Would not work for processes if the
> model was ever changed. Would make it easier for rogue packets to shut a
> worker down. Would not allow any way to distinguish between shutdown
> priorities (not something I have mentioned). Definitely feasible. I'll keep
> it in mind.

If you go to multiple processes, yeah, it probably wouldn't work *on
Windows*. On Unix, you can fork and have multiple processes with the
same socket. (It's very common to have, for instance, a subprocess's
stdin/stdout/stderr linked to pipes of some sort; the calling process
still retains control.)

It would be _harder_ for rogue packets to shut a worker down this way.

>> TCP sockets work on the basis of a master socket and any number of
>> spawned sockets. The master is what gives you an open port; each
>> spawned socket represents one connection with one client. Once you
>> have an established connection, the master should be able to be closed
>> without disrupting that. No other process will be able to connect to
>> you, but you'll still be able to use one end of the socket to make the
>> other end readable.
>
> Agreed but I need the listening socket to remain open and listening for new
> connections (at least until the whole program is told to shut down).

Not sure why. The sole purpose of this socket is to establish a
(single) socket pair used for the termination signals - nothing more.
You shouldn't need to listen for any new connections, even if you
spawn new workers.

>> With UDP, any process that can send a UDP packet can flood the system
>> with them until your workers shut down. You wouldn't even notice until
>> it succeeds.
>
> Is that true? You seem to be describing a non-forged attack but to get the
> source UDP port right wouldn't the attacker have to be runing on the same
> machine *and* to bind to the same port that the machine had allocated to my
> program? I might be wrong but I don't think the UDP stack would allow the
> same port to be bound again before the original had been closed.

UDP basically doesn't have protections like that. TCP does, and though
it _is_ possible to forge packets, it requires raw socket access. I
don't know what protections Windows has around that, but certainly a
software firewall should be able to notice that some program is
spewing raw packets.

> Let's see. If I stick with my original plan then each worker would have a
> TCP socket and a UDP socket. The "listener" thread would have its single
> listening TCP socket plus it would have a UDP socket for each worker thread.
> Total of three sockets per worker, two of which would be UDP sockets with
> port numbers assigned and thus consumed.
>
> If I go with a single "shutdown socket" then I would have just one socket
> per worker. That would use fewer sockets, for sure.

Yeah, it's pretty cheap either way. Your most scarce resource here
would be UDP port numbers, and there's 64K of those. That'd let you go
as far as 32K workers, and I don't think you can have that many
threads without saturating something somewhere else.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Shutting down a cross-platform multithreaded app "James Harris" <james.harris.1@gmail.com> - 2015-09-18 18:17 +0100
  Re: Shutting down a cross-platform multithreaded app Paul Rubin <no.email@nospam.invalid> - 2015-09-18 11:23 -0700
    Re: Shutting down a cross-platform multithreaded app "James Harris" <james.harris.1@gmail.com> - 2015-09-18 20:09 +0100
      Re: Shutting down a cross-platform multithreaded app Laura Creighton <lac@openend.se> - 2015-09-18 22:50 +0200
        Re: Shutting down a cross-platform multithreaded app "James Harris" <james.harris.1@gmail.com> - 2015-09-19 10:56 +0100
  Re: Shutting down a cross-platform multithreaded app Marko Rauhamaa <marko@pacujo.net> - 2015-09-18 23:40 +0300
  Re: Shutting down a cross-platform multithreaded app Chris Angelico <rosuav@gmail.com> - 2015-09-19 07:40 +1000
    Re: Shutting down a cross-platform multithreaded app "James Harris" <james.harris.1@gmail.com> - 2015-09-19 10:49 +0100
      Re: Shutting down a cross-platform multithreaded app Chris Angelico <rosuav@gmail.com> - 2015-09-19 20:14 +1000
        Re: Shutting down a cross-platform multithreaded app "James Harris" <james.harris.1@gmail.com> - 2015-09-19 11:48 +0100
          Re: Shutting down a cross-platform multithreaded app Chris Angelico <rosuav@gmail.com> - 2015-09-19 20:59 +1000
  Re: Shutting down a cross-platform multithreaded app Random832 <random832@fastmail.com> - 2015-09-18 17:48 -0400
  Re: Shutting down a cross-platform multithreaded app Chris Angelico <rosuav@gmail.com> - 2015-09-19 08:09 +1000
  Re: Shutting down a cross-platform multithreaded app Akira Li <4kir4.1i@gmail.com> - 2015-09-19 02:56 +0300

csiph-web