Bug 215584

Summary: [webkitcorepy] Move Timeout to webkitcorepy
Product: WebKit Reporter: Jonathan Bedard <jbedard>
Component: Tools / TestsAssignee: Jonathan Bedard <jbedard>
Status: RESOLVED FIXED    
Severity: Normal CC: aakash_jain, darin, dewei_zhu, ews-watchlist, glenn, slewis, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
See Also: https://bugs.webkit.org/show_bug.cgi?id=214378
https://bugs.webkit.org/show_bug.cgi?id=215738
https://bugs.webkit.org/show_bug.cgi?id=215742
Attachments:
Description Flags
Patch
none
Patch
none
Patch
none
Patch for landing
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
dewei_zhu: review+
Patch
none
Patch none

Jonathan Bedard
Reported 2020-08-17 14:18:03 PDT
Webkitpy has a timeout class which uses alarms to raise an exception. This is a useful idea, although many functions which may block have their own native timeouts. To work around this, we should allow our timeout class to be disabled for a blocks that a program may use a more appropriate timeout for APIs which support it.
Attachments
Patch (25.41 KB, patch)
2020-08-17 14:30 PDT, Jonathan Bedard
no flags
Patch (26.31 KB, patch)
2020-08-18 11:10 PDT, Jonathan Bedard
no flags
Patch (26.29 KB, patch)
2020-08-19 16:07 PDT, Jonathan Bedard
no flags
Patch for landing (26.33 KB, patch)
2020-08-20 07:11 PDT, Jonathan Bedard
no flags
Patch (5.00 KB, patch)
2020-08-20 10:39 PDT, Jonathan Bedard
no flags
Patch (8.65 KB, patch)
2020-08-24 09:57 PDT, Jonathan Bedard
no flags
Patch (8.69 KB, patch)
2020-08-24 11:00 PDT, Jonathan Bedard
no flags
Patch (8.67 KB, patch)
2020-08-24 12:27 PDT, Jonathan Bedard
no flags
Patch (8.71 KB, patch)
2020-08-24 13:42 PDT, Jonathan Bedard
no flags
Patch (11.89 KB, patch)
2020-08-24 15:24 PDT, Jonathan Bedard
no flags
Patch (15.07 KB, patch)
2020-08-25 09:06 PDT, Jonathan Bedard
no flags
Patch (9.13 KB, patch)
2020-08-25 10:30 PDT, Jonathan Bedard
no flags
Patch (11.02 KB, patch)
2020-08-25 16:41 PDT, Jonathan Bedard
no flags
Patch (13.77 KB, patch)
2020-08-26 09:28 PDT, Jonathan Bedard
dewei_zhu: review+
Patch (13.75 KB, patch)
2020-08-26 21:17 PDT, Jonathan Bedard
no flags
Patch (4.97 KB, patch)
2020-08-27 07:16 PDT, Jonathan Bedard
no flags
Radar WebKit Bug Importer
Comment 1 2020-08-17 14:18:56 PDT
Jonathan Bedard
Comment 2 2020-08-17 14:30:27 PDT
Jonathan Bedard
Comment 3 2020-08-17 15:11:39 PDT
(In reply to Jonathan Bedard from comment #2) > Created attachment 406744 [details] > Patch Before landing this, want to be clear that this isn't a 100% move, while the new class is a drop in replacement to the old one, they aren't identical.
dewei_zhu
Comment 4 2020-08-17 16:02:20 PDT
Comment on attachment 406744 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=406744&action=review > Tools/Scripts/libraries/webkitcorepy/webkitcorepy/timeout.py:36 > + _process_to_timeout_map = {} How about using `collections.defaultdict(list)` to avoid boundary check? > Tools/Scripts/libraries/webkitcorepy/webkitcorepy/timeout.py:121 > + def sleep(cls, t): Nit: maybe call it 'seconds' to be clearer here. > Tools/Scripts/libraries/webkitcorepy/webkitcorepy/timeout.py:161 > + for i in range(len(self._process_to_timeout_map[os.getpid()]) - 1): > + if self.data.alarm_time < self._process_to_timeout_map[os.getpid()][i + 1].alarm_time: > + self._process_to_timeout_map[os.getpid()].insert(i, self.data) > + break It looks like we are doing a linear search on an sorted array, we can just use `bisect.insort` https://docs.python.org/3/library/bisect.html > Tools/Scripts/libraries/webkitcorepy/webkitcorepy/timeout.py:167 > + if current_timeout and current_timeout.alarm_time < self.data.alarm_time: > + for i in range(len(self._process_to_timeout_map[os.getpid()]) - 1): > + if self.data.alarm_time < self._process_to_timeout_map[os.getpid()][i + 1].alarm_time: > + self._process_to_timeout_map[os.getpid()].insert(i, self.data) > + break > + self._process_to_timeout_map[os.getpid()].append(self.data) > + > + # This is the most urgent timeout > + else: > + self._process_to_timeout_map[os.getpid()] = [self.data] + self._process_to_timeout_map.get(os.getpid(), []) > + This logic can be simplified to `bisect.insort(self._process_to_timeout_map[os.getpid()], self.data)` if we use defaultdict for self._process_to_timeout_map and implement `__cmp__` for Data class. For bisect.insort, you can refer to https://docs.python.org/3/library/bisect.html, it's basically upper_bound in cpp. > Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/timeout_unittest.py:31 > + Do we have a test case that an exception is raised in a Timeout context, and we expect the `_process_to_timeout_map` to be updated correctly? > Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/timeout_unittest.py:71 > + with mocks.Time: > + with OutputCapture() as capturer: > + with Timeout(1): > + Timeout.check() > + self.assertRaises(Timeout.Exception, time.sleep, 1) > + To avoid code getting too nested, I think we can just do ``` with mocks.Time, OutputCapture() as capturer, Timeout(1): ... ``` or ``` with mocks.Time, OutputCapture() as capturer: with Timeout(1): .... ```
Jonathan Bedard
Comment 5 2020-08-17 22:11:37 PDT
Comment on attachment 406744 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=406744&action=review >> Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/timeout_unittest.py:31 >> + > > Do we have a test case that an exception is raised in a Timeout context, and we expect the `_process_to_timeout_map` to be updated correctly? This is a good point. I've added a test that covers the "what happens when an exception has already been triggered and caught" case.
Jonathan Bedard
Comment 6 2020-08-18 11:10:05 PDT
Jonathan Bedard
Comment 7 2020-08-19 16:07:38 PDT
Jonathan Bedard
Comment 8 2020-08-20 07:11:35 PDT
Created attachment 406928 [details] Patch for landing
EWS
Comment 9 2020-08-20 07:34:02 PDT
Committed r265942: <https://trac.webkit.org/changeset/265942> All reviewed patches have been landed. Closing bug and clearing flags on attachment 406928 [details].
Jonathan Bedard
Comment 10 2020-08-20 10:39:06 PDT
Reopening to attach new patch.
Jonathan Bedard
Comment 11 2020-08-20 10:39:07 PDT
Jonathan Bedard
Comment 12 2020-08-20 17:22:15 PDT
(In reply to Jonathan Bedard from comment #11) > Created attachment 406945 [details] > Patch (Will be waiting to land this until next week)
Aakash Jain
Comment 13 2020-08-21 15:55:10 PDT
(In reply to EWS from comment #9) > Committed r265942: <https://trac.webkit.org/changeset/265942> For some reason, this seems to have broke ios-wk2 tests, see Bug 215742
Jonathan Bedard
Comment 14 2020-08-21 20:59:32 PDT
(In reply to Aakash Jain from comment #13) > (In reply to EWS from comment #9) > > Committed r265942: <https://trac.webkit.org/changeset/265942> > For some reason, this seems to have broke ios-wk2 tests, see Bug 215742 I’ll be revisiting this Monday. This is ultimately about the way that python’s multiprocess does forking, will take quite a bit of trial and error to track down.
Jonathan Bedard
Comment 15 2020-08-24 09:57:11 PDT
Jonathan Bedard
Comment 16 2020-08-24 11:00:02 PDT
Jonathan Bedard
Comment 17 2020-08-24 12:27:09 PDT
Jonathan Bedard
Comment 18 2020-08-24 13:42:44 PDT
Jonathan Bedard
Comment 19 2020-08-24 15:24:39 PDT
Jonathan Bedard
Comment 20 2020-08-25 09:06:37 PDT
Jonathan Bedard
Comment 21 2020-08-25 10:30:40 PDT
Jonathan Bedard
Comment 22 2020-08-25 16:41:49 PDT
Jonathan Bedard
Comment 23 2020-08-25 21:51:37 PDT
Comment on attachment 407249 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=407249&action=review > Tools/Scripts/webkitpy/xcode/simulated_device.py:642 > + with Timeout(timeout, handler=RuntimeError(u'Timed out waiting for process to open {} on {}'.format(bundle_id, self.udid)), patch=False): Took quite a bit of trial and error, but I found the original issue with the change, apparent mock.patch breaks future forking on MacOS, which is super surprising. The ultimate solution will be fixing our multiprocessing code, but that's not a change for this patch. All we need to do is opt out of patching time.sleep when booting simulators.
Jonathan Bedard
Comment 24 2020-08-26 09:28:20 PDT
Jonathan Bedard
Comment 25 2020-08-26 21:17:40 PDT
EWS
Comment 26 2020-08-26 22:43:07 PDT
Committed r266224: <https://trac.webkit.org/changeset/266224> All reviewed patches have been landed. Closing bug and clearing flags on attachment 407374 [details].
Jonathan Bedard
Comment 27 2020-08-27 07:16:12 PDT
Reopening to attach new patch.
Jonathan Bedard
Comment 28 2020-08-27 07:16:13 PDT
EWS
Comment 29 2020-08-28 08:21:35 PDT
Committed r266277: <https://trac.webkit.org/changeset/266277> All reviewed patches have been landed. Closing bug and clearing flags on attachment 407396 [details].
Note You need to log in before you can comment on or make changes to this bug.