BGE python thread or socket problem: script stopped between 2 lines #43990

Closed
opened 2015-03-14 09:24:40 +01:00 by jimy-byerley · 22 comments

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 BGE-bug-socket.zip

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

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 [BGE-bug-socket.zip](https://archive.blender.org/developer/F151233/BGE-bug-socket.zip) 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} ](http://blenderartists.org/forum/showthread.php?362226-Tron-R-reboot-reloaded-An-open-source-openworld-of-tron). jimy.byerley@gmail.com
Author

Changed status to: 'Open'

Changed status to: 'Open'
Author
Added subscribers: @jimy-byerley, @mont29, @JorgeBernalMartinez
Member

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

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
Author

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

There is an updated version of my test (now, client ip is detected automaticaly to work on localhost: client-test.blend). [BGE-bug-socket.zip](https://archive.blender.org/developer/F152178/BGE-bug-socket.zip)
Member

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.

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.
Author

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.

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.
Member

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Jorge Bernal self-assigned this 2015-03-17 14:54:32 +01:00
Member

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.

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.

Added subscriber: @dfelinto

Added subscriber: @dfelinto

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

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

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.

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.
Member

Changed status from 'Archived' to: 'Open'

Changed status from 'Archived' to: 'Open'
Member

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

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
Member

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

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

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

I found no solution yet, it is why I am here. But of course, if I find I will report it.
Jorge Bernal removed their assignment 2015-04-05 17:59:00 +02:00
Angus Hollands was assigned by Jorge Bernal 2015-05-17 02:54:07 +02:00
Member

Agoose, could you check it?

Agoose, could you check it?

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'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.

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Author

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 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).
Author

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

I use threads for accessing the BGE in my main project: [Tron-R ](http://blenderartists.org/forum/showthread.php?362226-Tron-R-reboot-reloaded-An-open-source-openworld-of-tron) And it never made any problem.

Added subscriber: @esoneson

Added subscriber: @esoneson

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.

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 ](http://stackoverflow.com/questions/41137366/python-client-recv-only-reciving-on-exit-inside-bge) assuming I where doing something wrong in my python code.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#43990
No description provided.