Repository: spesmilo/electrum
Issue: cannot send transactions with ledger nano hardware wallet

I am using electrum 2.7.18 from tarball on linux (debian stretch).

After clicking _send_ and clicking _yes_ to the "_proceed?_" question I get the dialog saying "_confirm transaction on your ledger device_" then a few seconds go by and it sometimes crashes, sometimes gets to the next step ("_signing transaction_" dialog). In all cases I just wait doing nothing.

What I'd expect is a new confirm request appearing on my android ledger app (as the _nano_ doesn't have a display like the _nano s_ does) so I can proceed, but I get nothing.

Ledger nano unlocks correctly and gets to green status (even though I can see my transaction history even before I enter the pin, which looks bad to me but is not the issue at hand).

Some example output I get in my terminal (where I launch electrum with `python electrum`) when it just crashes:
```
Traceback (most recent call last):
  File "/home/dieeasy/src/Electrum-2.7.18/plugins/ledger/qt.py", line 50, in auth_dialog
    self.message_dialog(e)
  File "/home/dieeasy/src/Electrum-2.7.18/plugins/ledger/qt.py", line 41, in message_dialog
    l = QLabel(msg)
TypeError: arguments did not match any overloaded call:
  QLabel(parent: QWidget = None, flags: Qt.WindowFlags = 0): argument 1 has unexpected type 'exceptions.ImportError'
  QLabel(QString, parent: QWidget = None, flags: Qt.WindowFlags = 0): argument 1 has unexpected type 'exceptions.ImportError'
[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
python: ../../src/xcb_io.c:259: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
Aborted
```

```
[xcb] Unknown request in queue while dequeuing
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
python: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.
python: ../../src/xcb_io.c:165: dequeue_pending_request: Assertion `!xcb_xlib_unknown_req_in_deq' failed.
Aborted
```

```
*** Error in `python': double free or corruption (fasttop): 0x00007fa394017800 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x70bcb)[0x7fa3ba9f3bcb]
/lib/x86_64-linux-gnu/libc.so.6(+0x76f96)[0x7fa3ba9f9f96]
/lib/x86_64-linux-gnu/libc.so.6(+0x7778e)[0x7fa3ba9fa78e]
/usr/lib/x86_64-linux-gnu/libX11.so.6(_XEventsQueued+0x55)[0x7fa393476b15]
/usr/lib/x86_64-linux-gnu/libX11.so.6(XEventsQueued+0x69)[0x7fa393468789]
/usr/lib/x86_64-linux-gnu/libQtGui.so.4(+0x26e01c)[0x7fa39db5b01c]
/lib/x86_64-linux-gnu/libglib-2.0.so.0(g_main_context_check+0x1d9)[0x7fa39c73f3f9]
/lib/x86_64-linux-gnu/libglib-2.0.so.0(+0x4a994)[0x7fa39c73f994]
/lib/x86_64-linux-gnu/libglib-2.0.so.0(g_main_context_iteration+0x2c)[0x7fa39c73fb0c]
/usr/lib/x86_64-linux-gnu/libQtCore.so.4(_ZN20QEventDispatcherGlib13processEventsE6QFlagsIN10QEventLoop17ProcessEventsFlagEE+0x86)[0x7fa39d5b5876]
/usr/lib/x86_64-linux-gnu/libQtGui.so.4(+0x26e5d6)[0x7fa39db5b5d6]
/usr/lib/x86_64-linux-gnu/libQtCore.so.4(_ZN10QEventLoop13processEventsE6QFlagsINS_17ProcessEventsFlagEE+0x3f)[0x7fa39d5837ef]
/usr/lib/x86_64-linux-gnu/libQtCore.so.4(_ZN10QEventLoop4execE6QFlagsINS_17ProcessEventsFlagEE+0x1b5)[0x7fa39d583b55]
/usr/lib/x86_64-linux-gnu/libQtCore.so.4(_ZN16QCoreApplication4execEv+0x99)[0x7fa39d589bd9]
/usr/lib/python2.7/dist-packages/PyQt4/QtGui.x86_64-linux-gnu.so(+0x5676db)[0x7fa39eb426db]
python(PyEval_EvalFrameEx+0x69a)[0x55bf0d5683ca]
python(PyEval_EvalCodeEx+0x255)[0x55bf0d566535]
python(PyEval_EvalFrameEx+0x6968)[0x55bf0d56e698]
python(PyEval_EvalFrameEx+0x5eef)[0x55bf0d56dc1f]
python(PyEval_EvalCodeEx+0x255)[0x55bf0d566535]
python(PyEval_EvalCode+0x19)[0x55bf0d5662d9]
python(+0x128e3f)[0x55bf0d595e3f]
python(PyRun_FileExFlags+0x82)[0x55bf0d590d52]
python(PyRun_SimpleFileExFlags+0x19e)[0x55bf0d59089e]
python(Py_Main+0x6c1)[0x55bf0d541af1]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1)[0x7fa3ba9a32b1]
python(_start+0x2a)[0x55bf0d54131a]
======= Memory map: ========
55bf0d46d000-55bf0d790000 r-xp 00000000 00:14 2728326                    /usr/bin/python2.7
55bf0d990000-55bf0d992000 r--p 00323000 00:14 2728326                    /usr/bin/python2.7
55bf0d992000-55bf0da09000 rw-p 00325000 00:14 2728326                    /usr/bin/python2.7
...
```

Example output when it gets to the "_signing transaction_" step:
```
Traceback (most recent call last):
  File "/home/dieeasy/src/Electrum-2.7.18/plugins/ledger/qt.py", line 50, in auth_dialog
    self.message_dialog(e)
  File "/home/dieeasy/src/Electrum-2.7.18/plugins/ledger/qt.py", line 41, in message_dialog
    l = QLabel(msg)
TypeError: arguments did not match any overloaded call:
  QLabel(parent: QWidget = None, flags: Qt.WindowFlags = 0): argument 1 has unexpected type 'exceptions.ImportError'
  QLabel(QString, parent: QWidget = None, flags: Qt.WindowFlags = 0): argument 1 has unexpected type 'exceptions.ImportError'
```


As an additional note, version 2.7.9 in debian repos can't send either but yields a dialog with just a "0" in it.

Changed files:
gui/qt/__init__.py

diff --git a/gui/qt/__init__.py b/gui/qt/__init__.py
index 8f55a772e..9490df7bf 100644
--- a/gui/qt/__init__.py
+++ b/gui/qt/__init__.py
@@ -82,6 +82,7 @@ class ElectrumGui:
         # GC-ed when windows are closed
         #network.add_jobs([DebugMem([Abstract_Wallet, SPV, Synchronizer,
         #                            ElectrumWindow], interval=5)])
+        QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads)
         self.config = config
         self.daemon = daemon
         self.plugins = plugins