Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset plugin state on crash in extension.snippet.render() #6

Merged
merged 1 commit into from May 7, 2021

Conversation

zoni
Copy link
Contributor

@zoni zoni commented May 7, 2021

Prior to this change, an exception in render would make the plugin hang on "Loading" when an exception like the following happens:

ulauncher-snippets | 2021-05-07 14:52:08,908 | DEBUG | ulauncher.api.client.Client: on_message() | Incoming event ItemEnterEvent
Traceback (most recent call last):
  File "/home/zoni/Workspace/zoni/ulauncher-snippets/main.py", line 57, in on_event
    snippet = extension.snippet.render(copy_mode=copy_mode)
  File "/home/zoni/Workspace/zoni/ulauncher-snippets/src/functions.py", line 103, in render
    template = Template(snippet.content)
  File "/usr/lib/python3.9/site-packages/jinja2/environment.py", line 1031, in __new__
    return env.from_string(source, template_class=cls)
  File "/usr/lib/python3.9/site-packages/jinja2/environment.py", line 941, in from_string
    return cls.from_code(self, self.compile(source), globals, None)
  File "/usr/lib/python3.9/site-packages/jinja2/environment.py", line 638, in compile
    self.handle_exception(source=source_hint)
  File "/usr/lib/python3.9/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/lib/python3.9/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateAssertionError: no filter named 'replace_with_symbol'

After closing the ulauncher window and then attempting to insert a new
snippet, it would hang without feedback because of corruption of its
internal state:

ulauncher-snippets | 2021-05-07 14:52:15,427 | DEBUG | ulauncher.api.client.Client: on_message() | Incoming event KeywordQueryEvent
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/ulauncher/api/client/Client.py", line 54, in on_message
    self.extension.trigger_event(event)
  File "/usr/lib/python3.9/site-packages/ulauncher/api/client/Extension.py", line 52, in trigger_event
    action = listener.on_event(event, self)
  File "/home/zoni/Workspace/zoni/ulauncher-snippets/main.py", line 82, in on_event
    show_var_input(extension.snippet, extension.variable, search)
AttributeError: 'SnippetsExtension' object has no attribute 'variable'

With this change, this code path is wrapped in try/except which will (1) display the exception message to the end-user and (2) consistently reset plugin state afterwards.

Prior to this change, an exception in render would make the plugin hang
on "Loading" when an exception like the following happens:

```
ulauncher-snippets | 2021-05-07 14:52:08,908 | DEBUG | ulauncher.api.client.Client: on_message() | Incoming event ItemEnterEvent
Traceback (most recent call last):
  File "/home/zoni/Workspace/zoni/ulauncher-snippets/main.py", line 57, in on_event
    snippet = extension.snippet.render(copy_mode=copy_mode)
  File "/home/zoni/Workspace/zoni/ulauncher-snippets/src/functions.py", line 103, in render
    template = Template(snippet.content)
  File "/usr/lib/python3.9/site-packages/jinja2/environment.py", line 1031, in __new__
    return env.from_string(source, template_class=cls)
  File "/usr/lib/python3.9/site-packages/jinja2/environment.py", line 941, in from_string
    return cls.from_code(self, self.compile(source), globals, None)
  File "/usr/lib/python3.9/site-packages/jinja2/environment.py", line 638, in compile
    self.handle_exception(source=source_hint)
  File "/usr/lib/python3.9/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/lib/python3.9/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateAssertionError: no filter named 'replace_with_symbol'
```

After closing the ulauncher window and then attempting to insert a new
snippet, it would hang without feedback because of corruption of its
internal state:

```
ulauncher-snippets | 2021-05-07 14:52:15,427 | DEBUG | ulauncher.api.client.Client: on_message() | Incoming event KeywordQueryEvent
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/ulauncher/api/client/Client.py", line 54, in on_message
    self.extension.trigger_event(event)
  File "/usr/lib/python3.9/site-packages/ulauncher/api/client/Extension.py", line 52, in trigger_event
    action = listener.on_event(event, self)
  File "/home/zoni/Workspace/zoni/ulauncher-snippets/main.py", line 82, in on_event
    show_var_input(extension.snippet, extension.variable, search)
AttributeError: 'SnippetsExtension' object has no attribute 'variable'
```

With this change, this code path is wrapped in try/except which will (1)
display the exception message to the end-user and (2) consistently reset
plugin state afterwards.
@zoni zoni mentioned this pull request May 7, 2021
@mikebarkmin mikebarkmin merged commit acee817 into mikebarkmin:main May 7, 2021
@mikebarkmin
Copy link
Owner

lgtm 🎆

@zoni zoni deleted the reset-state-on-render-crash branch May 7, 2021 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants