[New-bugs-announce] [issue45818] socketserver.BaseRequestHandler inherited class

Sergey M. report at bugs.python.org
Tue Nov 16 11:21:51 EST 2021


New submission from Sergey M. <seregaxvm.main at gmail.com>:

Due to 
```python
try:
    self.handle()
finally:
    self.finish()
```
construct in `socketserver.BaseRequestHandler.__init__()` method inherited classes with `overrided __init__()` method may suffer from incomplete initialization.
For example, in the following snippet
```python
def __init__(self, request, client_address, server):
    super().__init__(request, client_address, server)
    self.foo = 1
```
in some cases all the code after `super()` call will not be executed.

This is a MWE of the server with partially initialized Handler class
```python
from socketserver import UnixStreamServer, StreamRequestHandler, ForkingMixIn


class Handler(StreamRequestHandler):
    def __init__(self, request, client_address, server):
        super().__init__(request, client_address, server)
        self.foo = 1

    def handle(self):
        print(self.foo)


class ThreadedUnixStreamServer(ForkingMixIn, UnixStreamServer):
    pass


with ThreadedUnixStreamServer("/tmp/test.socket", Handler) as server:
    server.serve_forever()
```

----------
components: Library (Lib)
messages: 406413
nosy: matsievskiysv
priority: normal
severity: normal
status: open
title: socketserver.BaseRequestHandler inherited class
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45818>
_______________________________________


More information about the New-bugs-announce mailing list