BGE python thread or socket problem: script stopped between 2 lines
Closed, InvalidPublic

Description

I wrote a simple module to use easely the python TCP socket to create game. With it it is possible to synchronize some object's rotation, location, properties, ...
But when I test it, there is a problem.
BGE seems to be stopping the scrit during the execution or anything like this.
I start the server (server-test.py) on python 3.2 in a shell
I open client-test.blend with blender 2.73 and I launch the game.
the client connect himself to the server, and normaly sends sync requests, but the script is stopped just at line 46 of file client.py
and it continues when I stop the BGE !

Here is my work

the server console output is then :

[('192.168.1.10', 45619)]
{}
[('192.168.1.10', 45619)]
{}
[('192.168.1.10', 45619)]
{}
getpacket: 'registerloc\x00Cube'
getpacket: 'registerrot\x00Cube'
getpacket: 'registerprop\x00Cube\x00prop'
send 'setloc\x000\x000\x000\x00Cube'
send 'setrot\x000\x000\x000\x00Cube'
send 'setprop\x00Cube\x00prop\x00None'
send 'setloc\x000\x000\x000\x00Cube'
send 'setrot\x000\x000\x000\x00Cube'
send 'setprop\x00Cube\x00prop\x00None'
send 'setloc\x000\x000\x000\x00Cube'
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner
    self.run()
  File "/home/jimy/tron-reboot/server/BGE-bug-socket/server.py", line 140, in run
    if data.position : self.sendall('setloc\0%d\0%d\0%d\0%s' % (data.position[0], data.position[1], data.position[2], obj))
  File "/home/jimy/tron-reboot/server/BGE-bug-socket/basic_connection.py", line 206, in sendall
    self.send(i, data)
  File "/home/jimy/tron-reboot/server/BGE-bug-socket/basic_connection.py", line 202, in send
    self.tunnels[index].send('{}{}{}'.format(START_MARK, str(data), END_MARK).encode())
socket.error: [Errno 32] Broken pipe

[('192.168.1.10', 45619)]
{'Cube': {pos=(0, 0, 0),         rot=(0, 0, 0),  parent=False,   properties={'prop': None}}}
[('192.168.1.10', 45619)]
{'Cube': {pos=(0, 0, 0),         rot=(0, 0, 0),  parent=False,   properties={'prop': None}}}
^CTraceback (most recent call last):
  File "server-test.py", line 12, in <module>
    sleep(1)
KeyboardInterrupt

All is normal on this side : when the server receive the requests, it starts synching.

The client continue execution only after the BGE stopped (after Blender Game Engine Finished)
The client side get :

Blender Game Engine Started
send queued
Blender Game Engine Finished
queue
send 'registerloc\x00Cube'
queue
send 'registerrot\x00Cube'
queue
send 'registerprop\x00Cube\x00prop'
receiving
getpacket
no packet
send queued
receiving
getpacket
getpacket: 'setloc\x000\x000\x000\x00Cube'
confirmed
Writing: /tmp/client-test.crash.txt
/usr/local/bin/blender : ligne 2 : 17951 Erreur de segmentation  /usr/local/share/blender/blender "$@"

Please help me !
I am on a important game project and I need multiplayer (tron-R {reboot|reloaded}.

jimy.byerley@gmail.com

Details

Type
Bug
(jimy-byerley) updated the task description. (Show Details)
(jimy-byerley) raised the priority of this task from to Needs Triage.
(jimy-byerley) added a project: Game Engine.
(jimy-byerley) set Type to Bug.

I can not connect to the server.

Blender Game Engine Started
Python script error - object 'Cube', controller 'Python':
Traceback (most recent call last):
  File "setup", line 6, in <module>
  File "/home/lordloki/Pruebas/BGE-bug-socket/basic_connection.py", line 17, in __init__
    self.tunnel.connect(addr)
OSError: [Errno 113] No route to host
Blender Game Engine Finished

Anyway, this doesn't seem a BGE bug. It seems threading bug in the client-server code

There is an updated version of my test (now, client ip is detected automaticaly to work on localhost: client-test.blend).

You have the issue in file client.py line 49:

self.send(self.queue[0])

the crash comes from same file, line 59:

scene = bge.logic.getCurrentScene()

but it is normal because you want to accede to the scene after the game engine is finished.

Has you tried to get help from blenderartists? I don't see that the issue is related with bge itself.

My problem in not the crash of blender, but the pause in the script between line 46 and 47 (the client prints 'queue' only after the game was stopped by user.
I also tested it using Wireshark, no packet are sent before the client was stopped.

I will try to find some help on blenderartist, in case it is not a bug from BGE.

Jorge Bernal (lordloki) closed this task as Invalid.Mar 17 2015, 2:54 PM
Jorge Bernal (lordloki) claimed this task.

I saw at blenderartists that the issue was related to the use of blocking sockets instead of non-blocking sockets.

Then, I close the bug report as BGE is not affected.

Thanks for reporting.

people should ALWAYS use preemptive/non-blocking sockets with the BGE, since there is no real python threading available there

Don't close !
I use non blocking sockets for transmission (basic_connection.py : line 18) and anyway, it is when I send a mail that the script is stopped, but just between 2 code lines witout any network usage.

As you see up, script prints "send queued" from client.py line 46.
Then, the script was stopped. After "Game engine finished", the script prints "queue" from client.py line 48.
And so the 'Client.send()' method was not called before.

For me the scripts stops at line 47 from file client.py :

for i in range(len(self.queue)):

I didn't find this issue anywhere else, I it the reason why I report it.

Sorry for closing.

It was a coincident that someone asked for the same issue at the same time, at blenderartists: http://blenderartists.org/forum/showthread.php?365621-Sending-data-via-sockets

If you find a solution and the problem is not related to bge, please report it here.

I found no solution yet, it is why I am here. But of course, if I find I will report it.

Jorge Bernal (lordloki) triaged this task as Incomplete priority.May 15 2015, 3:14 PM

I've read through the code, and It's too hard to follow and quickly find a solution.
However, it seems pretty clear that this is simply a threading issue, which is probably due to a lock somewhere being held (or another thread taking all the processing). It's hard to discern from this file, but I've happily used threads in my own projects with no issue (TCP).

I would advise not to use boolean variables as locks, as they won't function correctly. Neither would I advise accessing the BGE API from a separate thread, as it won't function as expected, especially global state like getCurrentScene() / getCurrentController(). Instead use a queue (queue.Queue) to send data (position copy, orientation copy, ...) to/from BGE.

I recently tried blender 2.75 RC2, Blender doesn't crash when I press ESCAPE (but it is not the interesting point). I have the same problem : The script stops at the same line and continue only when BGE is ended (in blender interface).

I use threads for accessing the BGE in my main project: Tron-R
And it never made any problem.

I just ran in to this problem myself and can confirm that it still exists, It is also looking like it is a blender problem and not a bug in the threading as my code is working in a pure python console window.

to produce this problem you need to creat a thread, run it using .start(). Inside of the thread, try to use any tcp socket commands, the commands won't be executed until you exit blender GE. if you run the thread using .run() everything is working. this is as .run() don't start a new thread but instead run the content inside the current one (the main loop)

Before i found this thread I posted a question about this HERE assuming I where doing something wrong in my python code.