opensistemas-hub / osbrain

osBrain - A general-purpose multi-agent system module written in Python
https://osbrain.readthedocs.io/en/stable/
Apache License 2.0
175 stars 43 forks source link

Unable to close client connection #333

Closed bangxiangyong closed 5 years ago

bangxiangyong commented 5 years ago

It seems that the .close() function can only work on server socket address, when used on client socket address, the agent crashes.

Here's an example for PUB-SUB, where the Eve agent crashes in attempting to close the client socket.

May I know how to destroy/close a client socket if possible?

import time
from osbrain import run_agent
from osbrain import run_nameserver

def log_message(agent, message):
    agent.log_info('Received: %s' % message)

if __name__ == '__main__':

    # System deployment
    ns = run_nameserver()
    alice = run_agent('Alice')
    bob = run_agent('Bob')
    eve = run_agent('Eve')

    # System configuration
    addr = alice.bind('PUB', alias='main')
    bob.connect(addr,  handler=log_message)
    eve.connect(addr, alias = "BOBBY", handler=log_message)

    #this will crash - close connections
    eve.close("BOBBY")
    eve.close_all()

    # Send messages
    for _ in range(3):
        time.sleep(1)
        alice.send('main', 'Hello, all!')

    ns.shutdown()

Here's the Exception Error

ERROR [2019-06-30 00:22:09.920610] (Eve): An exception occurred while running! (Unknown error)

|>>>>>>>>Traceback (most recent call last):
|   File "F:\ProgramData\Anaconda3\lib\site-packages\osbrain\agent.py", line 1681, in run
|     self._loop()
|   File "F:\ProgramData\Anaconda3\lib\site-packages\osbrain\agent.py", line 1209, in _loop
|     if self._iterate():
|   File "F:\ProgramData\Anaconda3\lib\site-packages\osbrain\agent.py", line 1234, in _iterate
|     events = dict(self._poller.poll(self._poll_timeout))
|   File "F:\ProgramData\Anaconda3\lib\site-packages\zmq\sugar\poll.py", line 99, in poll
|     return zmq_poll(self.sockets, timeout=timeout)
|   File "zmq/backend/cython/_poll.pyx", line 143, in zmq.backend.cython._poll.zmq_poll
|   File "zmq/backend/cython/_poll.pyx", line 123, in zmq.backend.cython._poll.zmq_poll
|   File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
| zmq.error.ZMQError: Unknown error
|<<<<<<<<
Exception in thread oneway-call:
Traceback (most recent call last):
  File "F:\ProgramData\Anaconda3\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "F:\ProgramData\Anaconda3\lib\site-packages\Pyro4\core.py", line 1889, in run
    super(_OnewayCallThread, self).run()
  File "F:\ProgramData\Anaconda3\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "F:\ProgramData\Anaconda3\lib\site-packages\osbrain\agent.py", line 1681, in run
    self._loop()
  File "F:\ProgramData\Anaconda3\lib\site-packages\osbrain\agent.py", line 1209, in _loop
    if self._iterate():
  File "F:\ProgramData\Anaconda3\lib\site-packages\osbrain\agent.py", line 1234, in _iterate
    events = dict(self._poller.poll(self._poll_timeout))
  File "F:\ProgramData\Anaconda3\lib\site-packages\zmq\sugar\poll.py", line 99, in poll
    return zmq_poll(self.sockets, timeout=timeout)
  File "zmq/backend/cython/_poll.pyx", line 143, in zmq.backend.cython._poll.zmq_poll
  File "zmq/backend/cython/_poll.pyx", line 123, in zmq.backend.cython._poll.zmq_poll
  File "zmq/backend/cython/checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
zmq.error.ZMQError: Unknown error
Peque commented 5 years ago

@bangxiangyong Thanks for reporting this.

I tried your code and I am able to run it without any problems:

$ python asdf.py 
Broadcast server running on 0.0.0.0:9091
NS running on 127.0.0.1:14133 (127.0.0.1)
URI = PYRO:Pyro.NameServer@127.0.0.1:14133
INFO [2019-06-30 11:09:00.317774] (Bob): Received: Hello, all!
INFO [2019-06-30 11:09:01.319196] (Bob): Received: Hello, all!
INFO [2019-06-30 11:09:02.321479] (Bob): Received: Hello, all!
NS shut down.

Would you mind sharing more information about your environment?

bangxiangyong commented 5 years ago

Hi @Peque , many thanks for the reply:

here are the details:

Peque commented 5 years ago

@bangxiangyong Thanks for the detailed information.

I have tried with the same Python version and osBrain version you are using and still cannot reproduce the error. It may be related to your OS (Windows 8.1) or your environment. I do not have a Windows 8.1 system at hand for testing, so let us try to discard an environment problem first.

You have a huge load of packages installed in your environment, so let us try to reduce that. Can you create a new clean environment, install osBrain and try to reproduce the error?

With Conda you should be able to:

conda create -n osbrain python=3.7
conda activate osbrain
conda list --export  # Should show just a few packages installed (less than 20)
pip install osbrain
python yourscript.py

With pure Python:

python3 -m venv venv
source venv/bin/activate
pip install osbrain
python yourscript.py
bangxiangyong commented 5 years ago

I've tried using a new clean env as suggested but with no luck : same error persists.. maybe it's with my OS

For some reason it used python3.6.8 in creating the new env, anyways here are the details: List of packages:

# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: win-64
certifi=2019.6.16=py36_0
pip=19.1.1=py36_0
python=3.6.8=h9f7ef89_7
setuptools=41.0.1=py36_0
sqlite=3.28.0=he774522_0
vc=14.1=h0510ff6_4
vs2015_runtime=14.15.26706=h3a45250_4
wheel=0.33.4=py36_0
wincertstore=0.2=py36h7fe50ca_0

Full output:

Broadcast server running on 0.0.0.0:9091
NS running on 127.0.0.1:14728 (127.0.0.1)
URI = PYRO:Pyro.NameServer@127.0.0.1:14728
ERROR [2019-07-01 11:44:20.964223] (Eve): An exception occurred while running! (Unknown error)

|>>>>>>>>Traceback (most recent call last):
|   File "f:\ProgramData\Anaconda3\envs\osbrain_test_env\lib\site-packages\osbrain\agent.py", line 1680, in run
|     self._loop()
|   File "f:\ProgramData\Anaconda3\envs\osbrain_test_env\lib\site-packages\osbrain\agent.py", line 1208, in _loop
|     if self._iterate():
|   File "f:\ProgramData\Anaconda3\envs\osbrain_test_env\lib\site-packages\osbrain\agent.py", line 1233, in _iterate
|     events = dict(self._poller.poll(self._poll_timeout))
|   File "f:\ProgramData\Anaconda3\envs\osbrain_test_env\lib\site-packages\zmq\sugar\poll.py", line 99, in poll
|     return zmq_poll(self.sockets, timeout=timeout)
|   File "zmq\backend\cython\_poll.pyx", line 143, in zmq.backend.cython._poll.zmq_poll
|   File "zmq\backend\cython\_poll.pyx", line 123, in zmq.backend.cython._poll.zmq_poll
|   File "zmq\backend\cython\checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
| zmq.error.ZMQError: Unknown error
|<<<<<<<<
Exception in thread oneway-call:
Traceback (most recent call last):
  File "f:\ProgramData\Anaconda3\envs\osbrain_test_env\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "f:\ProgramData\Anaconda3\envs\osbrain_test_env\lib\site-packages\Pyro4\core.py", line 1889, in run
    super(_OnewayCallThread, self).run()
  File "f:\ProgramData\Anaconda3\envs\osbrain_test_env\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "f:\ProgramData\Anaconda3\envs\osbrain_test_env\lib\site-packages\osbrain\agent.py", line 1680, in run
    self._loop()
  File "f:\ProgramData\Anaconda3\envs\osbrain_test_env\lib\site-packages\osbrain\agent.py", line 1208, in _loop
    if self._iterate():
  File "f:\ProgramData\Anaconda3\envs\osbrain_test_env\lib\site-packages\osbrain\agent.py", line 1233, in _iterate
    events = dict(self._poller.poll(self._poll_timeout))
  File "f:\ProgramData\Anaconda3\envs\osbrain_test_env\lib\site-packages\zmq\sugar\poll.py", line 99, in poll
    return zmq_poll(self.sockets, timeout=timeout)
  File "zmq\backend\cython\_poll.pyx", line 143, in zmq.backend.cython._poll.zmq_poll
  File "zmq\backend\cython\_poll.pyx", line 123, in zmq.backend.cython._poll.zmq_poll
  File "zmq\backend\cython\checkrc.pxd", line 25, in zmq.backend.cython.checkrc._check_rc
zmq.error.ZMQError: Unknown error

INFO [2019-07-01 11:44:21.964303] (Bob): Received: Hello, all!
INFO [2019-07-01 11:44:22.964375] (Bob): Received: Hello, all!
INFO [2019-07-01 11:44:23.964440] (Bob): Received: Hello, all!
NS shut down.
Peque commented 5 years ago

@bangxiangyong Yeah, your new clean environment looks just fine. Using Python 3.6.8 is okay too.

It may be related to your OS then. Note that Windows 8.1 mainstream support ended on January 9, 2018 (succeeded by Windows 10). As I do not have a Windows 8.1 system at hand I may not be able to help you. Do you have another OS at hand to try? (like Windows 10, or Linux)

I see there was another maybe-similar issue (with an Unknown error exception and under Windows 8), but it was closed without information on what changed in their environment.

bangxiangyong commented 5 years ago

@Peque ,

I don't have Win10 at the moment, i might try on a virtual machine with Linux .

I'll update here if it worked for me but it'll take awhile. much thanks

ocaballeror commented 5 years ago

Just tried this on a fresh conda environment on Windows 10, and I can confirm it fails too.

I'm still not sure why it fails, but I'll look into it.

ocaballeror commented 5 years ago

Self notes:

Peque commented 5 years ago

@ocaballeror Thanks for having a look at this.

Does it change if: