blob: d59eb8f210c7e166f5edf057f7039b45a4992bbf [file] [log] [blame]
Guido van Rossum27b7c7e2013-10-17 13:40:50 -07001"""Synchronization primitives."""
2
Yury Selivanov6370f342017-12-10 18:36:12 -05003__all__ = ('Lock', 'Event', 'Condition', 'Semaphore', 'BoundedSemaphore')
Guido van Rossum27b7c7e2013-10-17 13:40:50 -07004
5import collections
Andrew Svetlov68b34a72019-05-16 17:52:10 +03006import types
Andrew Svetlov28d8d142017-12-09 20:00:05 +02007import warnings
Guido van Rossum27b7c7e2013-10-17 13:40:50 -07008
9from . import events
10from . import futures
Andrew Svetlov0baa72f2018-09-11 10:13:04 -070011from . import exceptions
Andrew Svetlov68b34a72019-05-16 17:52:10 +030012from .import coroutines
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070013
14
Guido van Rossumab3c8892014-01-25 16:51:57 -080015class _ContextManager:
16 """Context manager.
17
18 This enables the following idiom for acquiring and releasing a
19 lock around a block:
20
21 with (yield from lock):
22 <block>
23
24 while failing loudly when accidentally using:
25
26 with lock:
27 <block>
Andrew Svetlov88743422017-12-11 17:35:49 +020028
29 Deprecated, use 'async with' statement:
30 async with lock:
31 <block>
Guido van Rossumab3c8892014-01-25 16:51:57 -080032 """
33
34 def __init__(self, lock):
35 self._lock = lock
36
37 def __enter__(self):
38 # We have no use for the "as ..." clause in the with
39 # statement for locks.
40 return None
41
42 def __exit__(self, *args):
43 try:
44 self._lock.release()
45 finally:
46 self._lock = None # Crudely prevent reuse.
47
48
Yury Selivanovd08c3632015-05-13 15:15:56 -040049class _ContextManagerMixin:
50 def __enter__(self):
51 raise RuntimeError(
52 '"yield from" should be used as context manager expression')
53
54 def __exit__(self, *args):
55 # This must exist because __enter__ exists, even though that
56 # always raises; that's how the with-statement works.
57 pass
58
Andrew Svetlov68b34a72019-05-16 17:52:10 +030059 @types.coroutine
Yury Selivanovd08c3632015-05-13 15:15:56 -040060 def __iter__(self):
61 # This is not a coroutine. It is meant to enable the idiom:
62 #
63 # with (yield from lock):
64 # <block>
65 #
66 # as an alternative to:
67 #
68 # yield from lock.acquire()
69 # try:
70 # <block>
71 # finally:
72 # lock.release()
Andrew Svetlov88743422017-12-11 17:35:49 +020073 # Deprecated, use 'async with' statement:
74 # async with lock:
75 # <block>
Andrew Svetlov28d8d142017-12-09 20:00:05 +020076 warnings.warn("'with (yield from lock)' is deprecated "
77 "use 'async with lock' instead",
78 DeprecationWarning, stacklevel=2)
Yury Selivanovd08c3632015-05-13 15:15:56 -040079 yield from self.acquire()
80 return _ContextManager(self)
81
Andrew Svetlov68b34a72019-05-16 17:52:10 +030082 # The flag is needed for legacy asyncio.iscoroutine()
83 __iter__._is_coroutine = coroutines._is_coroutine
84
Andrew Svetlov5f841b52017-12-09 00:23:48 +020085 async def __acquire_ctx(self):
86 await self.acquire()
Victor Stinner3f438a92017-11-28 14:43:52 +010087 return _ContextManager(self)
Yury Selivanovd08c3632015-05-13 15:15:56 -040088
Andrew Svetlov5f841b52017-12-09 00:23:48 +020089 def __await__(self):
Andrew Svetlov28d8d142017-12-09 20:00:05 +020090 warnings.warn("'with await lock' is deprecated "
91 "use 'async with lock' instead",
92 DeprecationWarning, stacklevel=2)
Andrew Svetlov5f841b52017-12-09 00:23:48 +020093 # To make "with await lock" work.
94 return self.__acquire_ctx().__await__()
95
96 async def __aenter__(self):
97 await self.acquire()
Victor Stinner3f438a92017-11-28 14:43:52 +010098 # We have no use for the "as ..." clause in the with
99 # statement for locks.
100 return None
Yury Selivanovd08c3632015-05-13 15:15:56 -0400101
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200102 async def __aexit__(self, exc_type, exc, tb):
Victor Stinner3f438a92017-11-28 14:43:52 +0100103 self.release()
Yury Selivanovd08c3632015-05-13 15:15:56 -0400104
105
106class Lock(_ContextManagerMixin):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700107 """Primitive lock objects.
108
109 A primitive lock is a synchronization primitive that is not owned
110 by a particular coroutine when locked. A primitive lock is in one
111 of two states, 'locked' or 'unlocked'.
112
113 It is created in the unlocked state. It has two basic methods,
114 acquire() and release(). When the state is unlocked, acquire()
115 changes the state to locked and returns immediately. When the
116 state is locked, acquire() blocks until a call to release() in
117 another coroutine changes it to unlocked, then the acquire() call
118 resets it to locked and returns. The release() method should only
119 be called in the locked state; it changes the state to unlocked
120 and returns immediately. If an attempt is made to release an
121 unlocked lock, a RuntimeError will be raised.
122
123 When more than one coroutine is blocked in acquire() waiting for
124 the state to turn to unlocked, only one coroutine proceeds when a
125 release() call resets the state to unlocked; first coroutine which
126 is blocked in acquire() is being processed.
127
Andrew Svetlov88743422017-12-11 17:35:49 +0200128 acquire() is a coroutine and should be called with 'await'.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700129
Andrew Svetlov88743422017-12-11 17:35:49 +0200130 Locks also support the asynchronous context management protocol.
131 'async with lock' statement should be used.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700132
133 Usage:
134
135 lock = Lock()
136 ...
Andrew Svetlov88743422017-12-11 17:35:49 +0200137 await lock.acquire()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700138 try:
139 ...
140 finally:
141 lock.release()
142
143 Context manager usage:
144
145 lock = Lock()
146 ...
Andrew Svetlov88743422017-12-11 17:35:49 +0200147 async with lock:
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700148 ...
149
150 Lock objects can be tested for locking state:
151
152 if not lock.locked():
Andrew Svetlov88743422017-12-11 17:35:49 +0200153 await lock.acquire()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700154 else:
155 # lock is acquired
156 ...
157
158 """
159
160 def __init__(self, *, loop=None):
161 self._waiters = collections.deque()
162 self._locked = False
163 if loop is not None:
164 self._loop = loop
165 else:
166 self._loop = events.get_event_loop()
167
168 def __repr__(self):
169 res = super().__repr__()
170 extra = 'locked' if self._locked else 'unlocked'
171 if self._waiters:
Yury Selivanov6370f342017-12-10 18:36:12 -0500172 extra = f'{extra}, waiters:{len(self._waiters)}'
173 return f'<{res[1:-1]} [{extra}]>'
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700174
175 def locked(self):
Victor Stinnerc37dd612013-12-02 14:31:16 +0100176 """Return True if lock is acquired."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700177 return self._locked
178
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200179 async def acquire(self):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700180 """Acquire a lock.
181
182 This method blocks until the lock is unlocked, then sets it to
183 locked and returns True.
184 """
Guido van Rossum83f5a382016-08-23 09:39:03 -0700185 if not self._locked and all(w.cancelled() for w in self._waiters):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700186 self._locked = True
187 return True
188
Yury Selivanov7661db62016-05-16 15:38:39 -0400189 fut = self._loop.create_future()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700190 self._waiters.append(fut)
Bar Harel2f79c012018-02-03 00:04:00 +0200191
192 # Finally block should be called before the CancelledError
193 # handling as we don't want CancelledError to call
194 # _wake_up_first() and attempt to wake up itself.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700195 try:
Bar Harel2f79c012018-02-03 00:04:00 +0200196 try:
197 await fut
198 finally:
199 self._waiters.remove(fut)
Andrew Svetlov0baa72f2018-09-11 10:13:04 -0700200 except exceptions.CancelledError:
Mathieu Sornay894a6542017-06-09 22:17:40 +0200201 if not self._locked:
202 self._wake_up_first()
203 raise
Bar Harel2f79c012018-02-03 00:04:00 +0200204
205 self._locked = True
206 return True
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700207
208 def release(self):
209 """Release a lock.
210
211 When the lock is locked, reset it to unlocked, and return.
212 If any other coroutines are blocked waiting for the lock to become
213 unlocked, allow exactly one of them to proceed.
214
215 When invoked on an unlocked lock, a RuntimeError is raised.
216
217 There is no return value.
218 """
219 if self._locked:
220 self._locked = False
Mathieu Sornay894a6542017-06-09 22:17:40 +0200221 self._wake_up_first()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700222 else:
223 raise RuntimeError('Lock is not acquired.')
224
Mathieu Sornay894a6542017-06-09 22:17:40 +0200225 def _wake_up_first(self):
Bar Harel2f79c012018-02-03 00:04:00 +0200226 """Wake up the first waiter if it isn't done."""
227 try:
228 fut = next(iter(self._waiters))
229 except StopIteration:
230 return
231
232 # .done() necessarily means that a waiter will wake up later on and
233 # either take the lock, or, if it was cancelled and lock wasn't
234 # taken already, will hit this again and wake up a new waiter.
235 if not fut.done():
236 fut.set_result(True)
Mathieu Sornay894a6542017-06-09 22:17:40 +0200237
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700238
239class Event:
Guido van Rossum994bf432013-12-19 12:47:38 -0800240 """Asynchronous equivalent to threading.Event.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700241
242 Class implementing event objects. An event manages a flag that can be set
243 to true with the set() method and reset to false with the clear() method.
244 The wait() method blocks until the flag is true. The flag is initially
245 false.
246 """
247
248 def __init__(self, *, loop=None):
249 self._waiters = collections.deque()
250 self._value = False
251 if loop is not None:
252 self._loop = loop
253 else:
254 self._loop = events.get_event_loop()
255
256 def __repr__(self):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700257 res = super().__repr__()
Guido van Rossumccea0842013-11-04 13:18:19 -0800258 extra = 'set' if self._value else 'unset'
259 if self._waiters:
Yury Selivanov6370f342017-12-10 18:36:12 -0500260 extra = f'{extra}, waiters:{len(self._waiters)}'
261 return f'<{res[1:-1]} [{extra}]>'
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700262
263 def is_set(self):
Victor Stinnerc37dd612013-12-02 14:31:16 +0100264 """Return True if and only if the internal flag is true."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700265 return self._value
266
267 def set(self):
268 """Set the internal flag to true. All coroutines waiting for it to
269 become true are awakened. Coroutine that call wait() once the flag is
270 true will not block at all.
271 """
272 if not self._value:
273 self._value = True
274
275 for fut in self._waiters:
276 if not fut.done():
277 fut.set_result(True)
278
279 def clear(self):
280 """Reset the internal flag to false. Subsequently, coroutines calling
281 wait() will block until set() is called to set the internal flag
282 to true again."""
283 self._value = False
284
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200285 async def wait(self):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700286 """Block until the internal flag is true.
287
288 If the internal flag is true on entry, return True
289 immediately. Otherwise, block until another coroutine calls
290 set() to set the flag to true, then return True.
291 """
292 if self._value:
293 return True
294
Yury Selivanov7661db62016-05-16 15:38:39 -0400295 fut = self._loop.create_future()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700296 self._waiters.append(fut)
297 try:
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200298 await fut
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700299 return True
300 finally:
301 self._waiters.remove(fut)
302
303
Yury Selivanovd08c3632015-05-13 15:15:56 -0400304class Condition(_ContextManagerMixin):
Guido van Rossum994bf432013-12-19 12:47:38 -0800305 """Asynchronous equivalent to threading.Condition.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700306
307 This class implements condition variable objects. A condition variable
308 allows one or more coroutines to wait until they are notified by another
309 coroutine.
Guido van Rossumccea0842013-11-04 13:18:19 -0800310
311 A new Lock object is created and used as the underlying lock.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700312 """
313
Andrew Svetlovf21fcd02014-07-26 17:54:34 +0300314 def __init__(self, lock=None, *, loop=None):
Guido van Rossumccea0842013-11-04 13:18:19 -0800315 if loop is not None:
316 self._loop = loop
317 else:
318 self._loop = events.get_event_loop()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700319
Andrew Svetlovf21fcd02014-07-26 17:54:34 +0300320 if lock is None:
321 lock = Lock(loop=self._loop)
322 elif lock._loop is not self._loop:
323 raise ValueError("loop argument must agree with lock")
324
Guido van Rossumccea0842013-11-04 13:18:19 -0800325 self._lock = lock
326 # Export the lock's locked(), acquire() and release() methods.
327 self.locked = lock.locked
328 self.acquire = lock.acquire
329 self.release = lock.release
330
331 self._waiters = collections.deque()
332
333 def __repr__(self):
334 res = super().__repr__()
335 extra = 'locked' if self.locked() else 'unlocked'
336 if self._waiters:
Yury Selivanov6370f342017-12-10 18:36:12 -0500337 extra = f'{extra}, waiters:{len(self._waiters)}'
338 return f'<{res[1:-1]} [{extra}]>'
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700339
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200340 async def wait(self):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700341 """Wait until notified.
342
343 If the calling coroutine has not acquired the lock when this
344 method is called, a RuntimeError is raised.
345
346 This method releases the underlying lock, and then blocks
347 until it is awakened by a notify() or notify_all() call for
348 the same condition variable in another coroutine. Once
349 awakened, it re-acquires the lock and returns True.
350 """
Guido van Rossumccea0842013-11-04 13:18:19 -0800351 if not self.locked():
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700352 raise RuntimeError('cannot wait on un-acquired lock')
353
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700354 self.release()
355 try:
Yury Selivanov7661db62016-05-16 15:38:39 -0400356 fut = self._loop.create_future()
Guido van Rossumccea0842013-11-04 13:18:19 -0800357 self._waiters.append(fut)
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700358 try:
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200359 await fut
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700360 return True
361 finally:
Guido van Rossumccea0842013-11-04 13:18:19 -0800362 self._waiters.remove(fut)
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700363
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700364 finally:
Yury Selivanovc92bf832016-06-11 12:00:07 -0400365 # Must reacquire lock even if wait is cancelled
Bar Harel57465102018-02-14 11:18:11 +0200366 cancelled = False
Yury Selivanovc92bf832016-06-11 12:00:07 -0400367 while True:
368 try:
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200369 await self.acquire()
Yury Selivanovc92bf832016-06-11 12:00:07 -0400370 break
Andrew Svetlov0baa72f2018-09-11 10:13:04 -0700371 except exceptions.CancelledError:
Bar Harel57465102018-02-14 11:18:11 +0200372 cancelled = True
373
374 if cancelled:
Andrew Svetlov0baa72f2018-09-11 10:13:04 -0700375 raise exceptions.CancelledError
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700376
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200377 async def wait_for(self, predicate):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700378 """Wait until a predicate becomes true.
379
380 The predicate should be a callable which result will be
381 interpreted as a boolean value. The final predicate value is
382 the return value.
383 """
384 result = predicate()
385 while not result:
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200386 await self.wait()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700387 result = predicate()
388 return result
389
390 def notify(self, n=1):
391 """By default, wake up one coroutine waiting on this condition, if any.
392 If the calling coroutine has not acquired the lock when this method
393 is called, a RuntimeError is raised.
394
395 This method wakes up at most n of the coroutines waiting for the
396 condition variable; it is a no-op if no coroutines are waiting.
397
398 Note: an awakened coroutine does not actually return from its
399 wait() call until it can reacquire the lock. Since notify() does
400 not release the lock, its caller should.
401 """
Guido van Rossumccea0842013-11-04 13:18:19 -0800402 if not self.locked():
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700403 raise RuntimeError('cannot notify on un-acquired lock')
404
405 idx = 0
Guido van Rossumccea0842013-11-04 13:18:19 -0800406 for fut in self._waiters:
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700407 if idx >= n:
408 break
409
410 if not fut.done():
411 idx += 1
412 fut.set_result(False)
413
414 def notify_all(self):
415 """Wake up all threads waiting on this condition. This method acts
416 like notify(), but wakes up all waiting threads instead of one. If the
417 calling thread has not acquired the lock when this method is called,
418 a RuntimeError is raised.
419 """
Guido van Rossumccea0842013-11-04 13:18:19 -0800420 self.notify(len(self._waiters))
421
Guido van Rossumccea0842013-11-04 13:18:19 -0800422
Yury Selivanovd08c3632015-05-13 15:15:56 -0400423class Semaphore(_ContextManagerMixin):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700424 """A Semaphore implementation.
425
426 A semaphore manages an internal counter which is decremented by each
427 acquire() call and incremented by each release() call. The counter
428 can never go below zero; when acquire() finds that it is zero, it blocks,
429 waiting until some other thread calls release().
430
Serhiy Storchaka14867992014-09-10 23:43:41 +0300431 Semaphores also support the context management protocol.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700432
Guido van Rossum085869b2013-11-23 15:09:16 -0800433 The optional argument gives the initial value for the internal
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700434 counter; it defaults to 1. If the value given is less than 0,
435 ValueError is raised.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700436 """
437
Guido van Rossum085869b2013-11-23 15:09:16 -0800438 def __init__(self, value=1, *, loop=None):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700439 if value < 0:
Guido van Rossum9c55a582013-11-21 11:07:45 -0800440 raise ValueError("Semaphore initial value must be >= 0")
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700441 self._value = value
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700442 self._waiters = collections.deque()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700443 if loop is not None:
444 self._loop = loop
445 else:
446 self._loop = events.get_event_loop()
447
448 def __repr__(self):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700449 res = super().__repr__()
Yury Selivanov6370f342017-12-10 18:36:12 -0500450 extra = 'locked' if self.locked() else f'unlocked, value:{self._value}'
Guido van Rossumccea0842013-11-04 13:18:19 -0800451 if self._waiters:
Yury Selivanov6370f342017-12-10 18:36:12 -0500452 extra = f'{extra}, waiters:{len(self._waiters)}'
453 return f'<{res[1:-1]} [{extra}]>'
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700454
Guido van Rossumd455a502015-09-29 11:54:45 -0700455 def _wake_up_next(self):
456 while self._waiters:
457 waiter = self._waiters.popleft()
458 if not waiter.done():
459 waiter.set_result(None)
460 return
461
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700462 def locked(self):
463 """Returns True if semaphore can not be acquired immediately."""
Guido van Rossumab3c8892014-01-25 16:51:57 -0800464 return self._value == 0
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700465
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200466 async def acquire(self):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700467 """Acquire a semaphore.
468
469 If the internal counter is larger than zero on entry,
470 decrement it by one and return True immediately. If it is
471 zero on entry, block, waiting until some other coroutine has
472 called release() to make it larger than 0, and then return
473 True.
474 """
Guido van Rossumd455a502015-09-29 11:54:45 -0700475 while self._value <= 0:
Yury Selivanov7661db62016-05-16 15:38:39 -0400476 fut = self._loop.create_future()
Guido van Rossumd455a502015-09-29 11:54:45 -0700477 self._waiters.append(fut)
478 try:
Andrew Svetlov5f841b52017-12-09 00:23:48 +0200479 await fut
Guido van Rossumd455a502015-09-29 11:54:45 -0700480 except:
481 # See the similar code in Queue.get.
482 fut.cancel()
483 if self._value > 0 and not fut.cancelled():
484 self._wake_up_next()
485 raise
486 self._value -= 1
487 return True
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700488
489 def release(self):
490 """Release a semaphore, incrementing the internal counter by one.
491 When it was zero on entry and another coroutine is waiting for it to
492 become larger than zero again, wake up that coroutine.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700493 """
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700494 self._value += 1
Guido van Rossumd455a502015-09-29 11:54:45 -0700495 self._wake_up_next()
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700496
Guido van Rossum085869b2013-11-23 15:09:16 -0800497
498class BoundedSemaphore(Semaphore):
499 """A bounded semaphore implementation.
500
501 This raises ValueError in release() if it would increase the value
502 above the initial value.
503 """
504
505 def __init__(self, value=1, *, loop=None):
506 self._bound_value = value
507 super().__init__(value, loop=loop)
508
509 def release(self):
510 if self._value >= self._bound_value:
511 raise ValueError('BoundedSemaphore released too many times')
512 super().release()