Keyboard Shortcuts

File
u :up to issue
m :publish + mail comments
M :edit review message
j / k :jump to file after / before current file
J / K :jump to next file with a comment after / before current file
Side-by-side diff
i :toggle intra-line diffs
e :expand all comments
c :collapse all comments
s :toggle showing all comments
n / p :next / previous diff chunk or comment
N / P :next / previous comment
<Up> / <Down> :next / previous line
<Enter> :respond to / edit current comment
d :mark current comment as done
Issue
u :up to list of issues
m :publish + mail comments
j / k :jump to patch after / before current patch
o / <Enter> :open current patch in side-by-side view
i :open current patch in unified diff view
Issue List
j / k :jump to issue after / before current issue
o / <Enter> :open current issue
# : close issue
Comment/message editing
<Ctrl> + s or <Ctrl> + Enter :save comment
<Esc> :cancel edit
Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(6)
Issues Repositories Search
Open Issues | Closed Issues | All Issues | Sign in with your Google Account to create issues and add comments

Issue 69870048: Future.set_result is not safe

Can't Edit
Can't Publish+Mail
Start Review
Created:
11 years, 10 months ago by Nikolay Kim
Modified:
11 years, 6 months ago
Reviewers:
GvR, haypo_gmail
Visibility:
Public.
Future.set_result is not safe

Patch Set 1 #

Patch Set 2 : Use helper function #

Total comments: 5
Created: 11 years, 10 months ago
Download [raw] [tar.bz2]
Unified diffs Side-by-side diffs Delta from patch set Stats (+51 lines, -8 lines) Patch
M asyncio/events.py View 1 1 chunk +7 lines, -0 lines 1 comment Download
M asyncio/proactor_events.py View 1 2 chunks +4 lines, -1 line 0 comments Download
M asyncio/queues.py View 1 1 chunk +3 lines, -1 line 0 comments Download
M asyncio/selector_events.py View 1 3 chunks +7 lines, -2 lines 1 comment Download
M asyncio/tasks.py View 1 1 chunk +3 lines, -1 line 0 comments Download
M asyncio/unix_events.py View 1 3 chunks +8 lines, -3 lines 2 comments Download
M tests/test_selector_events.py View 1 chunk +19 lines, -0 lines 1 comment Download
Total messages: 10
|
Nikolay Kim
Future.set_result is not safe to use with loop.call_soon() here is test for that issue: @mock.patch('asyncio.base_events.logger') ...
11 years, 10 months ago (2014年03月03日 22:03:25 UTC) #1
Future.set_result is not safe to use with loop.call_soon()
here is test for that issue:
 @mock.patch('asyncio.base_events.logger')
 def test_ctor_with_cancelled_waiter(self,m_log):
 fut = asyncio.Future(loop=self.loop)
 @asyncio.coroutine
 def foo():
 _SelectorSocketTransport(
 self.loop, self.sock, self.protocol, fut)
 yield from fut
 task = asyncio.async(foo(), loop=self.loop)
 test_utils.run_once(self.loop)
 task.cancel()
 test_utils.run_briefly(self.loop)
 self.assertTrue(fut.cancelled())
 # exception should not be raised
 self.assertFalse(m_log.error.called)
Sign in to reply to this message.
Nikolay Kim
On 2014年03月03日 22:03:25, Nikolay Kim wrote: > Future.set_result is not safe to use with loop.call_soon() ...
11 years, 10 months ago (2014年03月03日 22:04:52 UTC) #2
On 2014年03月03日 22:03:25, Nikolay Kim wrote:
> Future.set_result is not safe to use with loop.call_soon()
> 
> here is test for that issue:
> 
> @mock.patch('asyncio.base_events.logger')
> def test_ctor_with_cancelled_waiter(self,m_log):
> fut = asyncio.Future(loop=self.loop)
> 
> @asyncio.coroutine
> def foo():
> _SelectorSocketTransport(
> self.loop, self.sock, self.protocol, fut)
> yield from fut
> 
> task = asyncio.async(foo(), loop=self.loop)
> test_utils.run_once(self.loop)
> task.cancel()
> test_utils.run_briefly(self.loop)
> self.assertTrue(fut.cancelled())
> 
> # exception should not be raised
> self.assertFalse(m_log.error.called)
i see this problem in production
Sign in to reply to this message.
GvR
Hmm... Are you just concerned about the log messages or is the logic of your ...
11 years, 10 months ago (2014年03月03日 22:33:01 UTC) #3
Hmm... Are you just concerned about the log messages or is the logic of your app
compromised? Adding a new method to Future is a pretty drastic step (especially
since we are now past CPython 3.4 rc2).
Sign in to reply to this message.
GvR
I should add the behavior of set_result is intentional. Are you seeing these spurious log ...
11 years, 10 months ago (2014年03月03日 22:34:33 UTC) #4
I should add the behavior of set_result is intentional. Are you seeing these
spurious log message for all the places that you are patching? Maybe we should
have a discussion on the tracker first, or on python-tulip?
Sign in to reply to this message.
Nikolay Kim
On 2014年03月03日 22:33:01, GvR wrote: > Hmm... Are you just concerned about the log messages ...
11 years, 10 months ago (2014年03月03日 22:46:21 UTC) #5
On 2014年03月03日 22:33:01, GvR wrote:
> Hmm... Are you just concerned about the log messages or is the logic of your
app
> compromised? Adding a new method to Future is a pretty drastic step
(especially
> since we are now past CPython 3.4 rc2).
it doesnt compromised app logic, just annoying. and problem with Cancelled
exception, it is very hard to understand where it is from.
i do not insists on immediate fix, but it should be fixed eventually. 
Feb 28 00:28:03 ip-10-31-197-58 sqs-files ERROR [asyncio] Exception in callback
<bound method Future.set_result of Future<CANCELLED>>(None,)
handle: Handle(<bound method Future.set_result of Future<CANCELLED>>, (None,))
Traceback (most recent call last):
 File
"/usr/local/lib/python3.3/dist-packages/asyncio-0.4.1_p1-py3.3.egg/asyncio/events.py",
line 39, in _run
 self._callback(*self._args)
 File
"/usr/local/lib/python3.3/dist-packages/asyncio-0.4.1_p1-py3.3.egg/asyncio/futures.py",
line 298, in set_result
 raise InvalidStateError('{}: {!r}'.format(self._state, self))
asyncio.futures.InvalidStateError: CANCELLED: Future<CANCELLED>
Sign in to reply to this message.
Nikolay Kim
On 2014年03月03日 22:34:33, GvR wrote: > I should add the behavior of set_result is intentional. ...
11 years, 10 months ago (2014年03月03日 22:49:48 UTC) #6
On 2014年03月03日 22:34:33, GvR wrote:
> I should add the behavior of set_result is intentional. Are you seeing these
> spurious log message for all the places that you are patching? Maybe we should
> have a discussion on the tracker first, or on python-tulip?
sure, let's discuss on python-tulip
Sign in to reply to this message.
Nikolay Kim
On 2014年03月03日 22:49:48, Nikolay Kim wrote: > On 2014年03月03日 22:34:33, GvR wrote: > > I ...
11 years, 10 months ago (2014年03月04日 17:40:26 UTC) #7
On 2014年03月03日 22:49:48, Nikolay Kim wrote:
> On 2014年03月03日 22:34:33, GvR wrote:
> > I should add the behavior of set_result is intentional. Are you seeing these
> > spurious log message for all the places that you are patching? Maybe we
should
> > have a discussion on the tracker first, or on python-tulip?
> 
> sure, let's discuss on python-tulip
this patch uses helper function
Sign in to reply to this message.
GvR
A caveat on my suggestions: various tests fail if I do that. I haven't looked ...
11 years, 10 months ago (2014年03月05日 19:59:13 UTC) #8
A caveat on my suggestions: various tests fail if I do that. I haven't looked
into why yet.
Separately, I don't think you've added tests that reproduce the need for every
single call site you're modifying.
https://codereview.appspot.com/69870048/diff/20001/asyncio/selector_events.py
File asyncio/selector_events.py (right):
https://codereview.appspot.com/69870048/diff/20001/asyncio/selector_events.py...
asyncio/selector_events.py:434: futures.InvalidStateError, waiter.set_result,
None)
I'm curious if this one can't just drop the call_soon() step -- just go with
if not waiter.cancelled():
 waiter.set_result(None)
Same below.
https://codereview.appspot.com/69870048/diff/20001/asyncio/unix_events.py
File asyncio/unix_events.py (right):
https://codereview.appspot.com/69870048/diff/20001/asyncio/unix_events.py#new...
asyncio/unix_events.py:14: from . import events
Why not in alphabetical order?
https://codereview.appspot.com/69870048/diff/20001/asyncio/unix_events.py#new...
asyncio/unix_events.py:266: self._loop.call_soon(
Same comments here and below as for selector_events.py -- I'm not sure why these
use call_soon() at all.
Sign in to reply to this message.
haypo_gmail
The issue fixed by this patch can still reproduced in Tulip and Python 3.4/3.5. I ...
11 years, 6 months ago (2014年06月30日 14:10:24 UTC) #9
The issue fixed by this patch can still reproduced in Tulip and Python 3.4/3.5.
I opened in issue to discuss it:
http://bugs.python.org/issue21886 
Sign in to reply to this message.
haypo_gmail
https://codereview.appspot.com/69870048/diff/20001/asyncio/events.py File asyncio/events.py (right): https://codereview.appspot.com/69870048/diff/20001/asyncio/events.py#newcode20 asyncio/events.py:20: pass Instead of catching InvalidStateError, I would prefer to ...
11 years, 6 months ago (2014年06月30日 14:15:34 UTC) #10
https://codereview.appspot.com/69870048/diff/20001/asyncio/events.py
File asyncio/events.py (right):
https://codereview.appspot.com/69870048/diff/20001/asyncio/events.py#newcode20
asyncio/events.py:20: pass
Instead of catching InvalidStateError, I would prefer to check for cancelled
state:
def _maybe_set_result(fut, result):
 if fut.cancelled():
 return
 fut.set_result(result)
https://codereview.appspot.com/69870048/diff/20001/tests/test_selector_events.py
File tests/test_selector_events.py (right):
https://codereview.appspot.com/69870048/diff/20001/tests/test_selector_events...
tests/test_selector_events.py:704: fut = asyncio.Future(loop=self.loop)
I don't think that test_selector_events.py is the right place to test the patch.
The test is not reliable, it depends on the exact execution order of each
function.
IMO a test for the private helper function in test_futures.py should be enough.
Or no test at all? :-p
Sign in to reply to this message.
|
This is Rietveld f62528b

AltStyle によって変換されたページ (->オリジナル) /