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

Detect terminal text-based-browsers better and block them #1483

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Sushmey
Copy link
Collaborator

@Sushmey Sushmey commented Apr 15, 2024

What does this PR do, and why?

Opening links in browser for users using text-based browsers results in ZT being frozen indefinitely. This might be because of the loop to open link in browser clashing with urwid loop.

This PR blocks out text-browsers better than what it used to earlier. It does so by temporarily unsetting TERM environ so text-browsers aren't detected.
This results in an exception which displays an error for text-browsers.

External discussion & connections

How did you test this?

  • Manually - Behavioral changes
  • Manually - Visual changes
  • Adapting existing automated tests
  • Adding automated tests for new behavior (or missing tests)
  • Existing automated tests should already cover this (only a refactor of tested code)

Self-review checklist for each commit

  • It is a minimal coherent idea
  • It has a commit summary following the documented style (title & body)
  • It has a commit summary describing the motivation and reasoning for the change
  • It individually passes linting and tests
  • It contains test additions for any new behavior
  • It flows clearly from a previous branch commit, and/or prepares for the next commit

@zulipbot zulipbot added the size: S [Automatic label added by zulipbot] label Apr 15, 2024
Temporarily unsetting TERM environ so text-browsers aren't detected.

This results in an exception which displays an error for text-browsers.

Fixes zulip#1475.
Comment on lines +429 to +430
if isinstance(term, str):
os.environ["TERM"] = term # resetting
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This certainly shouldn't be in the exceptional block. As it is now, TERM will not be reset if there is no error. If you want something to happen in every case, you could use finally.

The simplest solution would be to reset TERM as soon as the browser is determined. If so, this may be clearer with two try blocks, so you can isolate the 'only-terminal-browsers' error from others.

That is, unless the webbrowser module does other detection of this kind after that point, which could require the environment to be set for later operations?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using finally is a very good idea!
Re using 2 try blocks, we don't need that since TERM environ isn't used anywhere during or after unsetting it. Resetting it in finally seems better it seems like the cleaner approach.

Comment on lines +413 to +414
term = os.environ.get("TERM") # Saving the original value
del os.environ["TERM"] # Temporarily deleting variable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like pop.

Comment on lines +430 to +431
# Set TERM environ to be able to check text-browsers
os.environ["TERM"] = "xterm-256color"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your test changes make the tests pass, but don't test the expectations from the new behavior:

  • is the environment variable unchanged?
  • can we test specifically what happens if there is no 'valid' webbrowser found?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused about writing the tests for this. The TERM environ doesn't really change, how do we test its transitory absence?
Also writing a test specifically for text_browser sounds good but again not sure what we can test. We can assert if TERM environ doesn't exist inos.environ but that's a value we are setting and not what we are fetching from the function.

@neiljp neiljp added the PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback label Apr 17, 2024
@Sushmey Sushmey changed the title Text-browsers cause ZT to freeze. Detect terminal text-based-browsers better and block them Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR awaiting update PR has been reviewed & is awaiting update or response to reviewer feedback size: S [Automatic label added by zulipbot]
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Opening URLs can freeze terminal (X11 but no GUI browser)
3 participants