Can this help solve the segmentation faults when importing python modules in plugin??

Please use template to report bugs and problems. Post here your questions when not sure where else to post
Only for bugs in the Domoticz application! other problems go in different subforums!
Forum rules
Before posting here, make sure you are on the latest Beta or Stable version.
If you have problems related to the web gui, clear your browser cache + appcache first.

Use the following template when posting here:

Version: xxxx
Platform: xxxx
Plugin/Hardware: xxxx
Description:
.....

If you are having problems with scripts/blockly, always post the script (in a spoiler or code tag) or screenshots of your blockly

If you are replying, please do not quote images/code from the first post

Please mark your topic as Solved when the problem is solved.
EscApe
Posts: 133
Joined: Thursday 02 April 2015 8:46
Target OS: Raspberry Pi
Domoticz version: v3+beta
Location: The Netherlands
Contact:

Can this help solve the segmentation faults when importing python modules in plugin??

Post by EscApe » Saturday 03 February 2018 13:17

Hi,

I have been searching for a solutions to the segmentation fault that crashes domoticz with some python plugins and found something that might be interesting to developers.

Why do soms modules import without problems and do others lead to a segmentation fault? This is one thing i noticed, but is't a little hard to explain so i'll use examples:

I can for example import (without crashing):
urlib (/<pythonibpath>/urllib/__init__.py
re (/<pythonibpath>/re.py)
http (/<pythonibpath>/http/__init__.py
xml.etree (/<pythonibpath>/xml/etree/__init__.py

But these will result in a segmentation fault:
urllib.request (/<pythonibpath>/urllib/request.py)
http.client (/<pythonibpath>/http/client.py)

A complicating factor is that there is no segmentation fault when i only install python3.4-dev. It starts when i install python3.5.
It seems like python in the plugin system cannot handle (sub) modules that reside inside a subdirectory of the main module and have their OWN filename (no __init__.py). The only modules that can be called by their filename are the ones in the main /usr/lib/pythonx directory.
(I did not test every single module, but more than i mentioned in the examples)

Could the way python (3.5) in domoticz searches for the corresponding .py file to a module name be the cause?

Is this of any help in hunting down this issue? Or did i stumble on something already known :-S?

User avatar
waaren
Posts: 606
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by waaren » Saturday 03 February 2018 13:37

EscApe wrote:
Saturday 03 February 2018 13:17
Hi,

I have been searching for a solutions to the segmentation fault that crashes domoticz with some python plugins and found something that might be interesting to developers.
......
Can this also be related to the fact that we don't see a segmentation fault when we compile locally ?
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave , dzVents 2.4.n, Amazon echo

blauwebuis
Posts: 315
Joined: Wednesday 21 December 2016 10:11
Target OS: Raspberry Pi
Domoticz version: current
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by blauwebuis » Friday 09 February 2018 12:19

Any thoughts on this?

I just had my first run-in with the segmentation fault.

DietPi (Stretch) with
Python 2.7
Python 3
Python 3.5

All plugins working except Smart Virtual Thermostat. It imports:
import Domoticz
import json
import urllib.parse as parse
import urllib.request as request
from datetime import datetime, timedelta
import time

But my own plugins are fine. They import:
import os
from subprocess import call
import time
import sys

As I understand it, a solution for now is to downgrade python 3.5 to 3.4?

Logread
Posts: 178
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi
Domoticz version:
Location: France
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by Logread » Saturday 10 February 2018 11:33

I also ran for the first time into this issue, as I was testing for the first time on one my my production systems a new plugin I wrote that uses the same urllib.request calls as my Virtual Smart thermostat plugin. So my finding so far is that:

Pi 3 Stretch with Domoticz 3.8564 (precompiled binary 2017-10-10): import urllib.request works
Pi Zero W Stretch with Domoticz 3.8702 (precompiled binary 2017-11-06): import urllib.request crashes with seg. fault 11
Raspbian Jessie running in VM on x86 PC with Domoticz 3.8160 compiled locally: import urllib.request works

I am investigating if using different python libraries (if possible for same purpose) could be a workaround and will report back

User avatar
heggink
Posts: 455
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi
Domoticz version: V3.9530
Location: NL
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by heggink » Tuesday 13 February 2018 1:29

@EscApe: confirmed. Tried the life360. Also includes urllib.presence and crashes domoticz. Tried MAC address presence and it works fine.
H
Pi3, latest beta
RFXCOM, z-wave (AEOTEC, switches, temhum, pir, contacts),
Plugwise2py, P1 'smart'meter & solar panel
Alexa, Wifi Cams motion detection
ESP: relays, PIR & Temp/TempHum
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest

blauwebuis
Posts: 315
Joined: Wednesday 21 December 2016 10:11
Target OS: Raspberry Pi
Domoticz version: current
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by blauwebuis » Tuesday 13 February 2018 12:12

Ok, so the conclusion seems to be that URLLIB is the culprit.

What's next?

febalci
Posts: 310
Joined: Monday 03 July 2017 19:58
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by febalci » Tuesday 13 February 2018 13:26

It might not necessarily to do with urllib itself but for some dependencies. I remember reading some forums regarding openssl problems with python. Is there anyone who can test if the segmentation fault is the outcome of ssl urllib requests (https://) or both (http:// and https://)?? I cannot test it since i use stable version domoticz and don't have this problem?

Or, you might just post here the version of your libssl, i guess the ones using stretch is having this more often since there was a libssl problem with stretch during domoticz installation??

blauwebuis
Posts: 315
Joined: Wednesday 21 December 2016 10:11
Target OS: Raspberry Pi
Domoticz version: current
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by blauwebuis » Tuesday 13 February 2018 14:23

Hmm. The Smart Virtual Thermostat..

- It opens a http link
- It uses the URLLIB Parse function to handle CSV lists.

- It does not open a https link though.

User avatar
heggink
Posts: 455
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi
Domoticz version: V3.9530
Location: NL
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by heggink » Thursday 15 February 2018 13:28

It IS urllib definitely. I took the bt-presence plugin, checked it to work (add device, all OK, deleted device). Then I just added import urllib.request at the start, restarted and added bt device again and, voila crash.

So adding an unused urllib.request import makes it crash.
Pi3, latest beta
RFXCOM, z-wave (AEOTEC, switches, temhum, pir, contacts),
Plugwise2py, P1 'smart'meter & solar panel
Alexa, Wifi Cams motion detection
ESP: relays, PIR & Temp/TempHum
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest

Logread
Posts: 178
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi
Domoticz version:
Location: France
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by Logread » Thursday 15 February 2018 15:51

Is is not only urllib... I tried several http handling libraries (http.client, urllib3, requests)... just importing any of these causes a crash. The first post in this thread by @EscApe definitely is the best explanation I have seen on this, but unfortunately adressing the issue is beyond my reach.

It is a pity as several of the plugins I wrote (including the Smart Virtual Thermostat) are really difficult (if not impossible ?) to adapt to the asynchronous "connections" model of the framework.

moroen
Posts: 165
Joined: Sunday 15 January 2017 12:06
Target OS: Linux
Domoticz version: beta
Location: Norway
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by moroen » Thursday 15 February 2018 20:41

Logread wrote:
Thursday 15 February 2018 15:51
Is is not only urllib... I tried several http handling libraries (http.client, urllib3, requests)... just importing any of these causes a crash. The first post in this thread by @EscApe definitely is the best explanation I have seen on this, but unfortunately adressing the issue is beyond my reach.

It is a pity as several of the plugins I wrote (including the Smart Virtual Thermostat) are really difficult (if not impossible ?) to adapt to the asynchronous "connections" model of the framework.
I've just taken a cursory glance at your Smart Virtual Thermostat, so it's likely I have missed something, but why are you relying on urllib or other http-handling libs? Why not use the http-transport that's built into the plugin-framework? Any plugin that relies on async operation, calling of threads etc. are almost certain to fail, as pointed out by Dnpwwo. Using the inbuilt transport gives you the possibility to send http-requests and process the result...

Regards,
M

Logread
Posts: 178
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi
Domoticz version:
Location: France
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by Logread » Friday 16 February 2018 19:50

moroen wrote:
Thursday 15 February 2018 20:41
I've just taken a cursory glance at your Smart Virtual Thermostat, so it's likely I have missed something, but why are you relying on urllib or other http-handling libs? Why not use the http-transport that's built into the plugin-framework? Any plugin that relies on async operation, calling of threads etc. are almost certain to fail, as pointed out by Dnpwwo. Using the inbuilt transport gives you the possibility to send http-requests and process the result...
@moroen,

Thanks for your input. I initially developed the Smart Virtual Thermostat using the built in http transport. But switched to urllib.request as it allowed a simpler code for my specific purpose (synchronous calls to the Domoticz Json API to read devices/uservariables not owned by the plugin, rather than event driven callbacks). And it worked flawlessly for months on my production systems (and for many other users as well), even with Debian Stretch and Python 3.5, until some time in late November or early December some commits were made to Domoticz that resulted in the issue we are discussing in this thread.

I however do not know which changes are causing the issue (I know @dnpwwo has worked around that time on fixing some instability issues and may be what we are experiencing here is an unintended consequence of fixes, but that's just pure speculation on my side...)

But indeed I am going to revert the Smart Virtual Thermostat (and some other plugins I wrote) to use the built-in http transport because I do not know if and when this import crash can be solved. If only because I (and other users of the Smart Virtual Thermostat I guess) will need to update domoticz at some point in time...

imcfarla
Posts: 47
Joined: Monday 04 December 2017 14:18
Target OS: Linux
Domoticz version:
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by imcfarla » Friday 16 February 2018 21:17

As far as i can see there is no way to set headers in the built in http transport.

For the Hive plugin I have written I need to be able to set some.

moroen
Posts: 165
Joined: Sunday 15 January 2017 12:06
Target OS: Linux
Domoticz version: beta
Location: Norway
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by moroen » Friday 16 February 2018 22:40

imcfarla wrote:
Friday 16 February 2018 21:17
As far as i can see there is no way to set headers in the built in http transport.

For the Hive plugin I have written I need to be able to set some.
From the wiki:

Code: Select all

            sendData = { 'Verb' : 'GET',
                         'URL'  : '/',
                         'Headers' : { 'Content-Type': 'text/xml; charset=utf-8', \
                                       'Connection': 'keep-alive', \
                                       'Accept': 'Content-Type: text/html; charset=UTF-8', \
                                       'Host': Parameters["Address"]+":"+Parameters["Port"], \
                                       'User-Agent':'Domoticz/1.0' }
                       }
            Connection.Send(sendData)
Regards,
M

Logread
Posts: 178
Joined: Sunday 28 August 2016 7:48
Target OS: Raspberry Pi
Domoticz version:
Location: France
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by Logread » Sunday 18 February 2018 20:01

Logread wrote:
Friday 16 February 2018 19:50
I am going to revert the Smart Virtual Thermostat (and some other plugins I wrote) to use the built-in http transport because I do not know if and when this import crash can be solved. If only because I (and other users of the Smart Virtual Thermostat I guess) will need to update domoticz at some point in time...
Done... the Smart Virtual Thermostat no longer imports urllib.request (interestingly it still imports urllib.parse but that does not seem to create any issue). Interested to hear feedback about how this rewrite solves the crash on startup.

But still what can be the root cause of this incompatibility of the urllib.request with some platforms ?

imcfarla
Posts: 47
Joined: Monday 04 December 2017 14:18
Target OS: Linux
Domoticz version:
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by imcfarla » Monday 19 February 2018 1:01

Lucky you - you can revert to using the internal Transport mechanism.

However when I try and connect to https://beekeeper.hivehome.com:443 I get an sslv3 handshake error. This is using the example code (changed to use HTTPS as the protocol and configured to use the site and port specified)

After some investigation this appears to be because the site uses SNI and domoticz does not send through the servername it is trying to connect to.
running:

Code: Select all

openssl s_client -connect beekeeper-uk.hivehome.com:443
fails with an SSL alert number 40 however:

Code: Select all

openssl s_client -connect beekeeper-uk.hivehome.com:443 -servername beekeeper-uk.hivehome.com
succeeds

Any pointers from anyone?

I think a

Code: Select all

SSL_set_tlsext_host_name(m_TLSSock.native_handle(), <hostname>);
needs to be added after line 387 in PluginTransports.cpp but I can't see how to get <hostname> in that routine.
This probably needs some qualification that the connection is a tls rather than ssl connection.

blauwebuis
Posts: 315
Joined: Wednesday 21 December 2016 10:11
Target OS: Raspberry Pi
Domoticz version: current
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by blauwebuis » Monday 19 February 2018 11:16

I've create a small guide on how to quickly deal with this issue on the wiki (and updated the rest of the python plugin page too):

https://www.domoticz.com/wiki/Using_Pyt ... leshooting

febalci
Posts: 310
Joined: Monday 03 July 2017 19:58
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by febalci » Monday 19 February 2018 13:22

Does that mean moving from Python 3.5 to 3.4 solves the issue with segmentation faults and urllib problems?

Quax1507
Posts: 122
Joined: Tuesday 07 April 2015 21:29
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by Quax1507 » Monday 19 February 2018 19:03

@Logread:

I upgraded to V 0.4.0 and it seems stable.

I also can no longer see the trange behaviour as described here: viewtopic.php?f=6&t=21339#p167277

Keeping fingers crossed...

imcfarla
Posts: 47
Joined: Monday 04 December 2017 14:18
Target OS: Linux
Domoticz version:
Contact:

Re: Can this help solve the segmentation faults when importing python modules in plugin??

Post by imcfarla » Monday 19 February 2018 23:16

This patch allow connections that require SNI to work

Code: Select all

diff --git a/hardware/plugins/PluginTransports.cpp b/hardware/plugins/PluginTransports.cpp
index 5110422e..dc6d7049 100644
--- a/hardware/plugins/PluginTransports.cpp
+++ b/hardware/plugins/PluginTransports.cpp
@@ -377,6 +377,7 @@ namespace Plugins {
 
                        m_TLSSock = new boost::asio::ssl::stream<boost::asio::ip::tcp::socket&>(*m_Socket, *m_Context);
                        m_TLSSock->lowest_layer().set_option(boost::asio::ip::tcp::no_delay(true));
+                       SSL_set_tlsext_host_name(m_TLSSock->native_handle(), m_IP.c_str());
 
                        //m_TLSSock->set_verify_mode(boost::asio::ssl::verify_peer);
                        m_TLSSock->set_verify_mode(boost::asio::ssl::verify_none);

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests