Commit Graph

23 Commits

Author SHA1 Message Date
Zane Bitter fef1c1543b Deprecate the ThreadGroup.cancel() API
This API is awkward, inefficient, incoherent, and unintuitive. The bug
fix for which it was originally added was abandoned in favour of a
different approach, so it was never used. It appears that no consumers
are currently calling it. It would be best if none started.

Add a deprecation warning to discourage its use and allow us to remove
it altogether at some point in the future.

Change-Id: I9559c7051024019fac957385faced645920b815c
2018-12-20 11:11:23 +13:00
Zane Bitter 55f897c613 Document the threadgroup module
There were no docstrings for this API, which as a bonus is extremely
subtle and poorly-designed. Document it so that users at least have a
fighting chance of using it to do the things they intended.

Change-Id: Id6677a7e5aaf8ec9ddeb597c6d5e8f97806e2248
2018-12-12 20:15:46 +13:00
Duc Truong 48c51fe9cd Add stop_on_exception to TG timers
ThreadGroup add_dynamic_timer_args and add_timer_args use
DynamicLoopingCall and FixedIntervalLoopingCall respectively.  Both
classes have support for stop_on_exception, but this parameter was not
exposed in ThreadGroup functions to create timers.

This change adds the missing stop_on_exception to the timer functions so
that ThreadGroup timers can continue on exceptions if the user chooses
to do so.

Change-Id: If03276f290e86e95ddc0b1d749b7460ed752b8ef
Co-Authored-By: Zane Bitter <zbitter@redhat.com>
2018-12-10 11:13:00 +13:00
Zane Bitter 750b51caaa Add better timer APIs to ThreadGroup
The ThreadGroup.add_timer() API is unintuitive, inflexible, and all
around pretty terrible. By allowing the caller to pass *args and
**kwargs, it strongly implies that you can write a wrapper like:

    def add_timer(self, interval, func, *args, **kwargs):
        self.group.add_timer(interval, func, *args, **kwargs)

and in fact at least 6 projects have done so (probably copying and
pasting from each other). But this is wrong, and will result in the
first positional arg intended for the callback function to be treated as
the initial_delay parameter and dropped from the list of arguments to be
passed to the callback when it is run. When called like this, the
initial_delay argument not only must be passed (preventing the caller
from relying on the default), interspersed between the callback function
and its arguments, but it must be passed as a positional and not a
keyword argument (preventing the caller from effectively documenting
what is going on):

        self.group.add_timer(interval, func, None, *args, **kwargs)

We are also unable to add any further options without breaking existing
consumers.

To improve the situation in the future, add a new add_timer_args() API
that takes the args and kwargs as individual sequence/mapping parameters
rather than as variadic parameters. The above call would become:

        self.group.add_timer_args(interval, func, args, kwargs)

and any optional parameters can be passed as keyword arguments. Any new
keyword arguments we might want to add can be safely added to this
method.

Calling the original add_timer() method with arguments (either
positional or keyword) intended for the callback function is now
deprecated. Those parameters could be removed in a future major release.

Change-Id: Ib2791342263e2b88c045bcc92adc8160f57a0ed6
2018-12-10 11:09:48 +13:00
TommyLike 20a2178171 [ThreadGroup] Don't remove timer when stop timers
Graceful exit is not working since we removed the timers
before waiting them to complete.

Closes-Bug: #1769825
Change-Id: I2e52b680a09d337d9b598a4826132df8c7b0b4d9
2018-05-09 00:54:54 +00:00
Duan Jiong d10c8fe38b Remove log translations
Log messages are no longer being translated. This removes all use of
the _LE, _LI, and _LW translation markers to simplify logging and to
avoid confusion with new contributions.

See:
http://lists.openstack.org/pipermail/openstack-i18n/2016-November/002574.html
http://lists.openstack.org/pipermail/openstack-dev/2017-March/113365.html

Change-Id: I52be02c3db254f63bb37270fa504694431d7671b
2017-03-23 16:04:59 +08:00
Thomas Herve e371821a01 Fix race condition with fast threads
When a green thread dies quickly, it will call the linked callbacks on
the link call synchronously. In this case, an error will happen when
ThreadGroup is used, as the thread is not accounted for until the link
is done. This manages this race condition by reverting the operations to
link after.

Change-Id: Iccf6edb0dfddda54552bd8787f64da84486061b7
Closes-Bug: #1662939
2017-02-08 17:26:28 +01:00
Joshua Harlow 9604fc9c43 Use a timeutils.StopWatch for cancel timing
Instead of using time.time and such directly the
oslo utils timeutils code provides a handy timer that
itself uses monotonic time (so it won't be affected
by time shifts) so prefer to use that for the timer like
code that exists in this new cancel code.

Change-Id: Ie41f8e90cf1cb3f62fd0def00b0bc7068acd2c40
2016-03-17 09:46:12 -07:00
Jason Dunsmore c2e6478761 Add ability to cancel Threads and ThreadGroups
Change-Id: I4d5eb361fd1820b4fe8501df6fe2a03c49e816b3
Partial-Bug: #1536451
2016-03-10 08:16:00 -06:00
Elena Ezhova 1c763f2285 Add Bandit to tox for security static analysis
Bandit is a tool designed to find common security issues in
Python code. This change adds a bandit target to tox.ini.

The config file bandit.yaml was generated using bandit-config-generator

Run bandit:
    tox -e bandit

Change-Id: I3bd64e1e58d4dc460ce869c50818e9913fe1b9b8
2015-12-01 16:10:15 +03:00
Marian Horban c70fa7e367 Code refactoring of ThreadGroup::stop_timers()
TrivialFix

Change-Id: I441422edbc0da463cd41681650a92746e36e61cc
2015-11-25 08:35:18 +00:00
Jenkins 8b2f286e04 Merge "Avoid removing entries for timers that didn't stop" 2015-11-11 13:50:18 +00:00
Jenkins 7ecf3bec34 Merge "Move the common thread manipulating routine to a shared routine" 2015-10-09 07:35:31 +00:00
Jenkins 055fe76201 Merge "Cleanup thread on thread done callback" 2015-10-06 05:29:00 +00:00
Joshua Harlow 63b4b60a9c Move the common thread manipulating routine to a shared routine
Change-Id: I758b458772b8b460783ad54bc88860fd125d1e62
2015-09-29 15:47:34 -07:00
Joshua Harlow 2e82520c92 Update log string to correctly denote what it waits on
Also updates error on waiting for thread log string to
match the other usages of log strings.

Change-Id: I9d1c04208f624e90d38c1064f3579d79a043cf9d
2015-09-29 15:41:41 -07:00
Joshua Harlow 3ae36f6eb9 Avoid removing entries for timers that didn't stop
It seems bad to remove timer objects for timers that
didn't actually stop, so instead of removing all the
timers (including the ones that had issues) only
remove the ones that didn't have issues.

Change-Id: I1dcb589f7f026ad8eaa9145a61c7c2c386ee438e
2015-09-29 15:36:30 -07:00
Joshua Harlow daea9a46bc Cleanup thread on thread done callback
There is no need to pass kwargs into this function
as it can already take positional arguments natively.

Passing them natively also allows for docstrings
to be associated with them and makes it easier to determine
the expected arguments (since the function definition can
be looked at to determine the arguments, vs. having to scan
the function body).

Change-Id: I809ce203b97995552cb6d4ad915a6ba63f71af18
2015-09-29 15:33:45 -07:00
apporc 9e81bbae05 ThreadGroup's stop didn't recognise the current thread correctly
Because x is a custom Thread instance, and threading.current_thread()
returns a GreenThread instance, they will never be a same instance.

This patch add one more property method for Thread class to identify itself.

Change-Id: I173a0a40b417d613cd270d42e40295075cd5b71f
Signed-off-by: apporc <appleorchard2000@gmail.com>
2015-09-17 10:38:05 +08:00
Elena Ezhova eea13af645 Enforce H405 check
Change-Id: I61170d3b8cebd6e3e381f33ddc52b6d0174efec4
2015-06-24 16:23:12 +03:00
Rohit Jaiswal 02242929f1 Return timer after adding it to internal list
In some cases, its useful to selectively stop
some timers and not all. This fix allows a way
for client applications to maintain a handle
on timers and be able to stop some of them
selectively.

This is needed by
https://review.openstack.org/#/c/190842/

Change-Id: Ib258c07d6dd12ef7cc9f4a6c8cbde885048ceef8
Closes-Bug: 1466661
2015-06-23 21:57:52 +00:00
Elena Ezhova 47d6f01abb Fix unit tests 2015-05-21 17:00:17 +03:00
Elena Ezhova dd2f334781 exported from oslo-incubator by graduate.sh 2015-05-21 16:51:40 +03:00