# Buggy code
def text(self, new_content: str):
    Gdk.threads_enter()
    self._clipboard.set_text(new_content, -1)
    Gdk.threads_leave()


# Traceback (RuntimeError)
2018-12-05 07:02:31,958 DEBUG - service - Raw key: 'b', modifiers: [], Key: b
2018-12-05 07:02:31,958 DEBUG - service - Window visible title: '*Unbenannt 1 - Mousepad', Window class: 'mousepad.Mousepad'
2018-12-05 07:02:31,958 DEBUG - service - No phrase/script matched hotkey
2018-12-05 07:02:31,958 DEBUG - service - Input stack at end of handle_keypress: deque(['b'], maxlen=150)
2018-12-05 07:02:32,127 DEBUG - service - Raw key: ' ', modifiers: [], Key:
2018-12-05 07:02:32,128 DEBUG - service - Window visible title: '*Unbenannt 1 - Mousepad', Window class: 'mousepad.Mousepad'
2018-12-05 07:02:32,128 DEBUG - service - No phrase/script matched hotkey
2018-12-05 07:02:32,132 DEBUG - iomediator - Send via clipboard
2018-12-05 07:02:32,133 DEBUG - service - Input stack at end of handle_keypress: deque([], maxlen=150)
2018-12-05 07:02:32,133 DEBUG - interface - Sending string via clipboard: a
2018-12-05 07:02:32,133 ERROR - service - Ignored locking error in handle_keypress
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/autokey/service.py", line 207, in __tryReleaseLock
self.configManager.lock.release()
RuntimeError: release unlocked lock
2018-12-05 07:02:32,135 DEBUG - interface - Sending via clipboard enqueued.
2018-12-05 07:02:32,135 DEBUG - interface - Send special key: [<Key.BACKSPACE: '<backspace>'>]
2018-12-05 07:02:32,137 DEBUG - interface - Send special key: [<Key.BACKSPACE: '<backspace>'>]
2018-12-05 07:02:32,139 DEBUG - iomediator - Send via event interface
2018-12-05 07:02:32,139 DEBUG - interface - Send modified key: modifiers: ['<shift>'] key: <insert>
2018-12-05 07:02:32,242 ERROR - interface - Error in X event loop thread
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/autokey/interface.py", line 230, in __eventLoop
method(*args)
File "/usr/lib/python3.7/site-packages/autokey/interface.py", line 626, in _restore_clipboard_text
self.clipboard.text = backup
File "/usr/lib/python3.7/site-packages/autokey/interface.py", line 161, in text
self._clipboard.set_text(new_content, -1)
TypeError: Argument 1 does not allow None as a value
2018-12-05 07:02:33,039 DEBUG - service - Raw key: 'b', modifiers: [], Key: b
2018-12-05 07:02:33,040 DEBUG - service - Window visible title: '*Unbenannt 1 - Mousepad', Window class: 'mousepad.Mousepad'
2018-12-05 07:02:33,040 DEBUG - service - No phrase/script matched hotkey
2018-12-05 07:02:33,041 DEBUG - service - Input stack at end of handle_keypress: deque(['b'], maxlen=150)
2018-12-05 07:02:33,318 DEBUG - service - Raw key: ' ', modifiers: [], Key:
2018-12-05 07:02:33,319 DEBUG - service - Window visible title: '*Unbenannt 1 - Mousepad', Window class: 'mousepad.Mousepad'
2018-12-05 07:02:33,319 DEBUG - service - No phrase/script matched hotkey
2018-12-05 07:02:33,322 DEBUG - iomediator - Send via clipboard
2018-12-05 07:02:33,323 DEBUG - service - Input stack at end of handle_keypress: deque([], maxlen=150)
2018-12-05 07:02:33,325 DEBUG - interface - Sending string via clipboard: a
2018-12-05 07:02:33,327 ERROR - service - Ignored locking error in handle_keypress
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/autokey/service.py", line 207, in __tryReleaseLock
self.configManager.lock.release()
RuntimeError: release unlocked lock
2018-12-05 07:02:33,328 DEBUG - interface - Sending via clipboard enqueued.
2018-12-05 07:02:33,329 DEBUG - interface - Send special key: [<Key.BACKSPACE: '<backspace>'>]
2018-12-05 07:02:33,333 DEBUG - interface - Send special key: [<Key.BACKSPACE: '<backspace>'>]

# Fixed code
def text(self, new_content: str):
    Gdk.threads_enter()
    try:
        # This call might fail and raise an Exception.
        # If it does, make sure to release the mutex and not deadlock AutoKey.
        self._clipboard.set_text(new_content, -1)
    finally:
        Gdk.threads_leave()