Lawouach / WebSocket-for-Python

WebSocket client and server library for Python 2 and 3 as well as PyPy (ws4py 0.5.1)
https://ws4py.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
1.12k stars 288 forks source link

message.data becoms None in spawned greenlets #194

Open dsuch opened 8 years ago

dsuch commented 8 years ago

Hi there,

I don't have a self-contained test case but I can easily reproduce the following nevertheless in my project.

In a subclass of ws4py.websocket.WebSocket that is running in a gevent-based server I implemented the received_message method.

This works fine as long as it looks similar to the below:

def received_message(self, message):
    _self._on_received_message(message.data)

However, the moment I spawn the handler function into its own greenlet as below ..

from gevent import spawn

def received_message(self, message):
    spawn(_self._on_received_message, message.data)

.. message.data becomes None somewhere between spawn and the moment _self._on_received_message gets called but at any rate _self._on_received_message receives None, the original data is lost by the time that method is called in its own greenlet.

The workaround is to deepcopy it before spawning the greenlet

from copy import deepcopy
from gevent import spawn

def received_message(self, message):
    spawn(_self._on_received_message, deepcopy(message.data))