0

I've recently run into issues running tornado on Mac OSX Yosemite. When making a request to the tornado server, I get the following traceback:

ERROR:tornado.general:Uncaught exception
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/tornado/http1connection.py", line 674, in _server_request_loop
    ret = yield conn.read_response(request_delegate)
  File "/Library/Python/2.7/site-packages/tornado/gen.py", line 628, in run
    value = future.result()
  File "/Library/Python/2.7/site-packages/tornado/concurrent.py", line 109, in result
    raise_exc_info(self._exc_info)
  File "/Library/Python/2.7/site-packages/tornado/gen.py", line 175, in wrapper
    yielded = next(result)
  File "/Library/Python/2.7/site-packages/tornado/http1connection.py", line 157, in _read_message
    max_bytes=self.params.max_header_size)
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 227, in read_until_regex
    self._try_inline_read()
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 673, in _try_inline_read
    self._add_io_state(ioloop.IOLoop.READ)
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 881, in _add_io_state
    self.fileno(), self._handle_events, self._state)
  File "/Library/Python/2.7/site-packages/tornado/ioloop.py", line 677, in add_handler
    self._impl.register(fd, events | self.ERROR)
  File "/Library/Python/2.7/site-packages/tornado/platform/kqueue.py", line 41, in register
    self._control(fd, events, select.KQ_EV_ADD)
  File "/Library/Python/2.7/site-packages/tornado/platform/kqueue.py", line 59, in _control
    kevents.append(select.kevent(
AttributeError: 'module' object has no attribute 'kevent'

The last line is the most important. I'm fairly certain this is just because of a missing brew package, but I'm at a loss as to what could I'm missing.

Even more mysteriously, I can import select and see that the kevent function exists.

Python 2.7.6 (default, Sep  9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import select
>>> select.kevent
<type 'select.kevent'>
Madison May
  • 2,723
  • 3
  • 22
  • 32
  • Hmm, that's very odd. You must have `select.kqueue` to reach this point, so you should have `select.kevent` as well. What does `import select; print(dir(select))` print for you? Also, you mention brew but these paths in `/Library` are the OSX system python, not brew. Maybe something is mixed up between brew and non-brew installations? – Ben Darnell Dec 05 '14 at 14:16
  • I've added a bit more to my question which further adds to the confusion. I think that's a reasonable assumption to make, that the system and brew installation have gotten entangled -- I'll look into it. – Madison May Dec 05 '14 at 16:31
  • I had forgotten to switch my default python to the brew version instead of the system version, but switching still doesn't seem to have resolved the problem. – Madison May Dec 05 '14 at 16:45
  • `which python` returns `usr/local/bin/python` – Madison May Dec 05 '14 at 16:45
  • 1
    And the paths in the stack trace are /Library even when `which python` is `/usr/local/bin/python`? That doesn't sound right, although I use macports instead of brew so I can't be of much more help here. (I've found macports to have better python support than homebrew, FWIW) – Ben Darnell Dec 05 '14 at 23:14
  • Yeah, the stack trace is identical, even when I use /usr/local/bin/python explicitly. – Madison May Dec 06 '14 at 06:19

1 Answers1

0

So I've come up with a temporary solution that I feel a bit guilty about even calling an answer, but here it is anyhow:

1) Open up /usr/local/lib/python2.7/site-packages/tornado/platform/kqueue.py (or similar). 2) Immediately after the import select, add the following line:

cached_kevent = select.kevent

3) Replace the following lines:

kevents.append(select.kevent(
    fd, filter=select.KQ_FILTER_READ, flags=flags))

with this modified version:

try:
    kevents.append(select.kevent(
        fd, filter=select.KQ_FILTER_READ, flags=flags))
except AttributeError:
    kevents.append(cached_kevent(
       fd, filter=select.KQ_FILTER_READ, flags=flags))

Any better answers would be much appreciated.

Madison May
  • 2,723
  • 3
  • 22
  • 32
  • A bit late, but we've just had a report of a similar issue on IPython. Is it possible that your code imported gevent (or something that used gevent)? Gevent appears to [remove select.kevent](https://github.com/gevent/gevent/blob/8412311e9b29c3317941a5306176faf535f6f4b1/src/gevent/monkey.py#L494). – Thomas K Feb 07 '17 at 11:58
  • Yeah, gevent was indeed imported in the same context I believe. – Madison May Feb 07 '17 at 16:33
  • That seems like a questionable design decision, but... I'm not all that surprised. – Madison May Feb 07 '17 at 16:33