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

fix: timeout context manager on Windows #13041

Merged
merged 2 commits into from
Feb 10, 2021

Conversation

betodealmeida
Copy link
Member

@betodealmeida betodealmeida commented Feb 10, 2021

SUMMARY

We currently use a context manager to timeout queries that run in SQL Lab, eg:

with utils.timeout(seconds=timeout, error_message=timeout_msg):
    data = sql_lab.get_sql_results(...)

The context manager uses SIGALRM, which is not available in Windows, resulting in an error (see #12824).

Even though Windows is not technically supported by Superset, this is a simple fix. I introduced a new timeout context manager based on a timer. This PR changes Superset to use that context manager only when running on Windows. In theory we could replace the old one, but I thought it would be safer to introduce the new one in parallel first.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A

TEST PLAN

I installed Python under a Windows VPS and tested the context manager with this script:

# test.py
import _thread
import threading


class Timeout:
    def __init__(self, seconds: int = 1, error_message: str = "Timeout") -> None:
        self.seconds = seconds
        self.error_message = error_message
        self.timer = threading.Timer(self.seconds, _thread.interrupt_main)

    def __enter__(self) -> None:
        self.timer.start()

    def __exit__(self, type, value, traceback) -> None:
        self.timer.cancel()
        if type is KeyboardInterrupt:
            raise Exception(self.error_message)


with Timeout():
    for i in range(int(1e15)):
        pass

(I didn't use sleep() because the _thread.interrupt_main doesn't interrupt it.)

Then running it causes the main thread to be aborted after 1 second:

C:\Users\Administrator\Documents>powershell -Command "Measure-Command {python test.py}"
Traceback (most recent call last):
  File "C:\Users\Administrator\Documents\test.py", line 21, in <module>
    pass
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Administrator\Documents\test.py", line 21, in <module>
    pass
  File "C:\Users\Administrator\Documents\test.py", line 16, in __exit__
    raise Exception(self.error_message)
Exception: Timeout


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 1
Milliseconds      : 463
Ticks             : 14631515
TotalDays         : 1.69346238425926E-05
TotalHours        : 0.000406430972222222
TotalMinutes      : 0.0243858583333333
TotalSeconds      : 1.4631515
TotalMilliseconds : 1463.1515




C:\Users\Administrator\Documents>

ADDITIONAL INFORMATION

  • Has associated issue: Error running SQL Lab #12824
  • Changes UI
  • Requires DB Migration.
  • Confirm DB Migration upgrade and downgrade tested.
  • Introduces new feature or API
  • Removes existing feature or API

@betodealmeida betodealmeida changed the title fix: timeout decorator in Windows fix: timeout decorator on Windows Feb 10, 2021
@codecov-io
Copy link

codecov-io commented Feb 10, 2021

Codecov Report

Merging #13041 (0b978ff) into master (2ce7982) will increase coverage by 13.93%.
The diff coverage is 57.14%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master   #13041       +/-   ##
===========================================
+ Coverage   53.06%   66.99%   +13.93%     
===========================================
  Files         489      489               
  Lines       17314    28794    +11480     
  Branches     4482        0     -4482     
===========================================
+ Hits         9187    19291    +10104     
- Misses       8127     9503     +1376     
Flag Coverage Δ
cypress ?
python 66.99% <57.14%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
superset/config.py 90.64% <ø> (ø)
...s/260bf0649a77_migrate_x_dateunit_in_time_range.py 0.00% <0.00%> (ø)
superset/utils/core.py 87.55% <53.33%> (ø)
superset/charts/commands/exceptions.py 92.85% <77.77%> (ø)
superset/utils/date_parser.py 96.87% <100.00%> (ø)
...t-frontend/src/dashboard/util/setPeriodicRunner.ts
...et-frontend/src/components/ConfirmStatusChange.tsx
...perset-frontend/src/views/CRUD/welcome/Welcome.tsx
...perset-frontend/src/dashboard/util/findParentId.js
...ore/components/controls/DateFilterControl/utils.ts
... and 968 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1a96102...0b978ff. Read the comment docs.

Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

LGTM - this has probably been the main barrier causing Superset no not be supported on Windows. Cautiously optimistic that we could start supporting this platform, too.

self, type: Any, value: Any, traceback: TracebackType
) -> None:
self.timer.cancel()
if type is KeyboardInterrupt: # raised by _thread.interrupt_main
Copy link
Member

Choose a reason for hiding this comment

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

Surprised that this is a KeyboardInterrupt

Copy link
Member Author

Choose a reason for hiding this comment

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

Me too!

@betodealmeida betodealmeida force-pushed the fix_timeout_windows branch 2 times, most recently from 927888f to 87b1bb1 Compare February 10, 2021 15:58
@betodealmeida betodealmeida changed the title fix: timeout decorator on Windows fix: timeout context manager on Windows Feb 10, 2021
@betodealmeida betodealmeida added the need:merge The PR is ready to be merged label Feb 10, 2021
@betodealmeida betodealmeida merged commit 7b8a0e1 into apache:master Feb 10, 2021
amitmiran137 pushed a commit to nielsen-oss/superset that referenced this pull request Feb 14, 2021
* fix: timeout decorator in Windows

* Fix lint
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 1.2.0 labels Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels need:merge The PR is ready to be merged size/M 🚢 1.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants