Thread with Pyjnuis object not detaching

134 views
Skip to first unread message

Dewald du Plooy

unread,
Apr 2, 2014, 6:28:18 PM4/2/14
to pyjni...@googlegroups.com

Hi,

I'm creating a thread in my python android application which reads and writes to Bluetooth. Bluetooth functionality is obtained through Pyjnius. Everything works OK, but when the thread closes I get the message:

native thread exited without detaching.

If I don't use Pyjnius, the thread opens and closes without problem.

I get the following output when the thread closes:

V/BluetoothSocket.cpp(10961): availableNative
V/BluetoothSocket.cpp(10961): abortNative
V/BluetoothSocket.cpp(10961): ...asocket_abort(57) complete
V/BluetoothSocket.cpp(10961): destroyNative
V/BluetoothSocket.cpp(10961): ...asocket_destroy(57) complete
D/dalvikvm(10961): threadid=13: thread exiting, not yet detached (count=0)
D/dalvikvm(10961): threadid=13: thread exiting, not yet detached (count=1)
E/dalvikvm(10961): threadid=13: native thread exited without detaching
E/dalvikvm(10961): VM aborting
F/libc    (10961): Fatal signal 11 (SIGSEGV) at 0x00bed330 (code=13805624)
I/DEBUG   (10957): SET
I/DEBUG   (10957): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG   (10957): Build fingerprint: 'samsung/espressorfxx/espressorf:4.0.4/IMM76D/P3100XWBLJ1:user/release-keys'
I/DEBUG   (10957): pid: 10961, tid: 10979  >>> org.kivy.pygame:python <<<
I/DEBUG   (10957): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr deadd00d
I/DEBUG   (10957):  r0 00000000  r1 00be2e58  r2 00000000  r3 00000000

So my questions are:

  1. Is there a specific way I should remove objects created with Pyjnius?
  2. May I use Pyjnius in threads?
Regards,
Dewald

LostCitizen

unread,
Apr 3, 2014, 6:05:47 PM4/3/14
to pyjni...@googlegroups.com

Can you post the code you're running?

Regards

--
You received this message because you are subscribed to the Google Groups "PyJNIus development ML" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyjnius-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dewa...@gmail.com

unread,
Apr 4, 2014, 1:08:05 PM4/4/14
to pyjni...@googlegroups.com
Hi LostCitizen,

Thank you for your interest in helping :)

I've attached the code so that you can easily test it on your device:
1. change "dev_name" variable to your PC name
2. copy "main.py" and "android.txt" to your device's kivy folder after
3. Run "pc_side_bt3.py" on your PC
4. Launch the kivy app with Kivy Launcher 1.8
5. Click on disconnect button
6. App will close in a few seconds. The output can be obtained with the "adb logcat" tool.

Here is the hopefully minimal example of my code:

from kivy.lang import Builder
from kivy.app import App
from kivy.logger import Logger

import time
import threading

from jnius import autoclass

BluetoothAdapter = autoclass('android.bluetooth.BluetoothAdapter')
BluetoothDevice = autoclass('android.bluetooth.BluetoothDevice')
BluetoothSocket = autoclass('android.bluetooth.BluetoothSocket')
UUID = autoclass('java.util.UUID')

dev_name = 'DEWALD-PC'
strUUID = '94f39d29-7d6d-437d-973b-fba39e49d4ee'

class CommLinkAndroidBT(object):

    def __init__(self, dev_name, strUUID):       
        self.dev_name = dev_name
        self.strUUID = strUUID
        self.open()
        
    def open(self):
        paired_devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices().toArray()
          
        for device in paired_devices:
            if device.getName() == self.dev_name:
                self.socket = device.createRfcommSocketToServiceRecord(UUID.fromString(self.strUUID))
                self.send_stream = self.socket.getOutputStream()
        self.socket.connect()
    
    def write(self, mesg):
        self.send_stream.write(mesg)        
        self.send_stream.flush()    #necessary?
        
    def close(self):
        self.socket.close()
        Logger.info('closing')
        
class UpdateThread(threading.Thread):

    def __init__(self, threadID, name, driver):
        threading.Thread.__init__(self)
        self.exitFlag = False
        self.commLink = CommLinkAndroidBT(dev_name, strUUID)

    def run(self):

        while not self.exitFlag:            
            self.commLink.write("A")
            time.sleep(1)

        self.commLink.close()

if __name__ == '__main__':
    kv = '''
BoxLayout:
    Button:
        text: 'Disconnect'
        on_release: app.disconnect()
    '''

    class Bluetooth(App):

        def on_start(self):            
            self.updateThread = UpdateThread(1, "UpdateThread", self)
            self.updateThread.start()            

        def build(self):
            return Builder.load_string(kv)                        

        def disconnect(self):
            Logger.info('Calling disconnect')
            self.updateThread.exitFlag = True
            self.updateThread.join()

    Bluetooth().run()






--
You received this message because you are subscribed to a topic in the Google Groups "PyJNIus development ML" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyjnius-dev/lCxC4If2Sbg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyjnius-dev...@googlegroups.com.
pc_side_bt3.py
android.txt
main.py

dewa...@gmail.com

unread,
Apr 9, 2014, 11:04:38 AM4/9/14
to pyjni...@googlegroups.com
Hi,

Ok, I've now confirmed that this has nothing to do with bluetooth. You can achieve a crash with any Pyjnius object. I will send a separate email for it shortly.

Best Regards,
Dewald


Brian Knapp

unread,
Sep 29, 2014, 11:30:18 AM9/29/14
to pyjni...@googlegroups.com
Was a solution reached?  I need to ship some code using this.

stephane...@gmail.com

unread,
Oct 28, 2014, 8:33:01 AM10/28/14
to pyjni...@googlegroups.com
Just getting started with Kivy on Android.  I have a pyjnius thread accessing USB resources and am running into this issue.  Right now I'm following the suggestion from here: https://github.com/kivy/pyjnius/issues/107
Essentially, don't exit a thread that uses pyjnius.

Brent Picasso

unread,
Nov 2, 2014, 9:12:46 PM11/2/14
to pyjni...@googlegroups.com
I'm running into this problem as well - it's proving to be a tremendous problem for our android app. I also commented on the github issue.

Any ideas how to resolve this issue? Might this approach help?

Any help is very much appreciated.
-Brent
Reply all
Reply to author
Forward
0 new messages