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

fixes a possible race condition in AutoRestartTrick #1002

Merged

Conversation

ivg
Copy link
Contributor

@ivg ivg commented Aug 10, 2023

Just a long shot for a failure observed on #998. My hypothesis is that when we stop ProcessWatcher before we restart the process manually, we don't yield to it and immediately kill the process. Next, when the ProcessWatcher thread is woken up, we have to conditions ready - the popen_obj and stopped_event, see the corresponding code,

 while True: if self.popen_obj.poll() is not None: 
     break 
  if self.stopped_event.wait(timeout=0.1): 
     return 

And despite that stopped_event is set, we first check for popen_obj and trigger the process restart.

We can also make the ProcessWatcher logic more robust, by checking if we are stopped before calling the termination callback, e.g.,

        try:
            if not self.stopped_event.is_set():
                self.process_termination_callback()
        except Exception:
            logger.exception("Error calling process termination callback")

I am not 100% sure about that, as I don't really know what semantics is expected from ProcessWatcher by other users. But at least the AutoRestarter expects this semantics - i.e., a watcher shall not call any events after it was stopped.

Just a long shot for a failure observed on gorakhargosh#998. My hypothesis is that
when we stop ProcessWatcher before we restart the process manually, we
don't yield to it and immediately kill the process. Next, when the
ProcessWatcher thread is woken up, we have to conditions ready - the
popen_obj and stopped_event, see the corresponding code, ``` while
True: if self.popen_obj.poll() is not None: break if
self.stopped_event.wait(timeout=0.1): return ```

And desipte that `stopped_event` is set, we first check for
`popen_obj` and trigger the process restart.

We can also make the ProcessWatcher logic more robust, by checking if
we are stopped before calling the termination callback, e.g.,

```
        try:
            if not self.stopped_event.is_set():
                self.process_termination_callback()
        except Exception:
            logger.exception("Error calling process termination callback")
```

I am not 100% sure about that, as I don't really know what semantics
is expected from ProcessWatcher by other users. But at least the
AutoRestarter expects this semantics - i.e., a watcher shall not call
any events after it was stopped.
i.e., don't send events if stopped
@ivg
Copy link
Contributor Author

ivg commented Aug 10, 2023

Okay, the first option didn't work, so I tried the second and the tests passed, at least on Linux, not sure what's going on with macOS, but it looks like it is a known issue, correct me if I am wrong.

@BoboTiG BoboTiG merged commit 7503d34 into gorakhargosh:master Jul 28, 2024
41 of 45 checks passed
@BoboTiG
Copy link
Collaborator

BoboTiG commented Jul 28, 2024

Thanks @ivg 🍾

github-actions bot pushed a commit to wxx9248/CIS-Game-Project-2023W that referenced this pull request Aug 12, 2024
Bumps [watchdog](https://github.com/gorakhargosh/watchdog) from 4.0.1 to
4.0.2.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/gorakhargosh/watchdog/releases">watchdog's
releases</a>.</em></p>
<blockquote>
<h2>4.0.2</h2>
<ul>
<li>Add support for Python 3.13 (<a
href="https://github.com/gorakhargosh/watchdog/issues/1052">#1052</a>)</li>
<li>[core] Run <code>ruff</code>, apply several fixes (<a
href="https://github.com/gorakhargosh/watchdog/issues/1033">#1033</a>)</li>
<li>[core] Remove execution rights from <code>events.py</code></li>
<li>[documentation] Update <code>PatternMatchingEventHandler</code>
docstrings (<a
href="https://github.com/gorakhargosh/watchdog/issues/1048">#1048</a>)</li>
<li>[documentation] Simplify the quickstart example (<a
href="https://github.com/gorakhargosh/watchdog/issues/1047">#1047</a>)</li>
<li>[fsevents] Add missing <code>event_filter</code> keyword-argument to
<code>FSEventsObserver.schedule()</code> (<a
href="https://github.com/gorakhargosh/watchdog/issues/1049">#1049</a>)</li>
<li>[utils] Fix a possible race condition in
<code>AutoRestartTrick</code> (<a
href="https://github.com/gorakhargosh/watchdog/issues/1002">#1002</a>)</li>
<li>[watchmedo] Remove execution rights from
<code>watchmedo.py</code></li>
</ul>
<p>:heart_decoration: Thanks to our beloved contributors: <a
href="https://github.com/BoboTiG"><code>@​BoboTiG</code></a>, <a
href="https://github.com/nbelakovski"><code>@​nbelakovski</code></a>, <a
href="https://github.com/ivg"><code>@​ivg</code></a></p>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/gorakhargosh/watchdog/blob/master/changelog.rst">watchdog's
changelog</a>.</em></p>
<blockquote>
<p>4.0.2</p>
<pre><code>
2024-08-11 • `full history
&lt;https://github.com/gorakhargosh/watchdog/compare/v4.0.1...v4.0.2&gt;`__
<ul>
<li>Add support for Python 3.13
(<code>[#1052](gorakhargosh/watchdog#1052)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1052&amp;gt;</code>__)</li>
<li>[core] Run <code>ruff</code>, apply several fixes
(<code>[#1033](gorakhargosh/watchdog#1033)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1033&amp;gt;</code>__)</li>
<li>[core] Remove execution rights from <code>events.py</code></li>
<li>[documentation] Update <code>PatternMatchingEventHandler</code>
docstrings
(<code>[#1048](gorakhargosh/watchdog#1048)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1048&amp;gt;</code>__)</li>
<li>[documentation] Simplify the quickstart example
(<code>[#1047](gorakhargosh/watchdog#1047)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1047&amp;gt;</code>__)</li>
<li>[fsevents] Add missing <code>event_filter</code> keyword-argument to
<code>FSEventsObserver.schedule()</code>
(<code>[#1049](gorakhargosh/watchdog#1049)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1049&amp;gt;</code>__)</li>
<li>[utils] Fix a possible race condition in
<code>AutoRestartTrick</code>
(<code>[#1002](gorakhargosh/watchdog#1002)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1002&amp;gt;</code>__)</li>
<li>[watchmedo] Remove execution rights from
<code>watchmedo.py</code></li>
<li>Thanks to our beloved contributors: <a
href="https://github.com/BoboTiG"><code>@​BoboTiG</code></a>, <a
href="https://github.com/nbelakovski"><code>@​nbelakovski</code></a>, <a
href="https://github.com/ivg"><code>@​ivg</code></a>
</code></pre></li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/9c5a43241f774930e958447b61a83b8bbd94fb21"><code>9c5a432</code></a>
Version 4.0.2</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/aac4328e593e8764c6a3face67961f955d64bb74"><code>aac4328</code></a>
chore: add git attributes file</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/6a33516b8cb87a04a2e208c7a794e58f40f6f9b9"><code>6a33516</code></a>
docs: tweak</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/cff604e90bc562dee16a32efbe6b4471d3e64105"><code>cff604e</code></a>
feat: Python 3.13 support (<a
href="https://github.com/gorakhargosh/watchdog/issues/1052">#1052</a>)</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/7503d34c1f49a96c7ab31b177f6f3a3ffc8ab049"><code>7503d34</code></a>
fix: possible race condition in <code>AutoRestartTrick</code> (<a
href="https://github.com/gorakhargosh/watchdog/issues/1002">#1002</a>)</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/7d4a369e82eea79ef281febb03d9267805f746bb"><code>7d4a369</code></a>
[core] Run ruff, apply several fixes (<a
href="https://github.com/gorakhargosh/watchdog/issues/1033">#1033</a>)</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/7cd723ac433781002062fdf6f6400043f0d9d55d"><code>7cd723a</code></a>
chore: partly move settings from <code>setup.cfg</code> to
<code>pyproject.toml</code></li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/654707e7e640d5b1d15e3714ba4ce54a3597bce9"><code>654707e</code></a>
fix: remove execution rights from <code>events.py</code>, and
<code>watchmedo.py</code>, files</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/4043ef098214732662950c06c2b39407804b3153"><code>4043ef0</code></a>
tests: improve flakyness + clean-up</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/206843c8bc22210c6bd95537fc28d87c552d03d0"><code>206843c</code></a>
chore: remove useless kwarg on <code>BaseObserver</code> subclasses</li>
<li>Additional commits viewable in <a
href="https://github.com/gorakhargosh/watchdog/compare/v4.0.1...v4.0.2">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=watchdog&package-manager=pip&previous-version=4.0.1&new-version=4.0.2)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
cdnninja pushed a commit to Hyundai-Kia-Connect/hyundai_kia_connect_api that referenced this pull request Aug 16, 2024
Bumps [watchdog](https://github.com/gorakhargosh/watchdog) from 4.0.1 to
4.0.2.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/gorakhargosh/watchdog/releases">watchdog's
releases</a>.</em></p>
<blockquote>
<h2>4.0.2</h2>
<ul>
<li>Add support for Python 3.13 (<a
href="https://github.com/gorakhargosh/watchdog/issues/1052">#1052</a>)</li>
<li>[core] Run <code>ruff</code>, apply several fixes (<a
href="https://github.com/gorakhargosh/watchdog/issues/1033">#1033</a>)</li>
<li>[core] Remove execution rights from <code>events.py</code></li>
<li>[documentation] Update <code>PatternMatchingEventHandler</code>
docstrings (<a
href="https://github.com/gorakhargosh/watchdog/issues/1048">#1048</a>)</li>
<li>[documentation] Simplify the quickstart example (<a
href="https://github.com/gorakhargosh/watchdog/issues/1047">#1047</a>)</li>
<li>[fsevents] Add missing <code>event_filter</code> keyword-argument to
<code>FSEventsObserver.schedule()</code> (<a
href="https://github.com/gorakhargosh/watchdog/issues/1049">#1049</a>)</li>
<li>[utils] Fix a possible race condition in
<code>AutoRestartTrick</code> (<a
href="https://github.com/gorakhargosh/watchdog/issues/1002">#1002</a>)</li>
<li>[watchmedo] Remove execution rights from
<code>watchmedo.py</code></li>
</ul>
<p>:heart_decoration: Thanks to our beloved contributors: <a
href="https://github.com/BoboTiG"><code>@​BoboTiG</code></a>, <a
href="https://github.com/nbelakovski"><code>@​nbelakovski</code></a>, <a
href="https://github.com/ivg"><code>@​ivg</code></a></p>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/gorakhargosh/watchdog/blob/master/changelog.rst">watchdog's
changelog</a>.</em></p>
<blockquote>
<p>4.0.2</p>
<pre><code>
2024-08-11 • `full history
&lt;https://github.com/gorakhargosh/watchdog/compare/v4.0.1...v4.0.2&gt;`__
<ul>
<li>Add support for Python 3.13
(<code>[#1052](gorakhargosh/watchdog#1052)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1052&amp;gt;</code>__)</li>
<li>[core] Run <code>ruff</code>, apply several fixes
(<code>[#1033](gorakhargosh/watchdog#1033)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1033&amp;gt;</code>__)</li>
<li>[core] Remove execution rights from <code>events.py</code></li>
<li>[documentation] Update <code>PatternMatchingEventHandler</code>
docstrings
(<code>[#1048](gorakhargosh/watchdog#1048)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1048&amp;gt;</code>__)</li>
<li>[documentation] Simplify the quickstart example
(<code>[#1047](gorakhargosh/watchdog#1047)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1047&amp;gt;</code>__)</li>
<li>[fsevents] Add missing <code>event_filter</code> keyword-argument to
<code>FSEventsObserver.schedule()</code>
(<code>[#1049](gorakhargosh/watchdog#1049)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1049&amp;gt;</code>__)</li>
<li>[utils] Fix a possible race condition in
<code>AutoRestartTrick</code>
(<code>[#1002](gorakhargosh/watchdog#1002)
&amp;lt;https://github.com/gorakhargosh/watchdog/pull/1002&amp;gt;</code>__)</li>
<li>[watchmedo] Remove execution rights from
<code>watchmedo.py</code></li>
<li>Thanks to our beloved contributors: <a
href="https://github.com/BoboTiG"><code>@​BoboTiG</code></a>, <a
href="https://github.com/nbelakovski"><code>@​nbelakovski</code></a>, <a
href="https://github.com/ivg"><code>@​ivg</code></a>
</code></pre></li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/9c5a43241f774930e958447b61a83b8bbd94fb21"><code>9c5a432</code></a>
Version 4.0.2</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/aac4328e593e8764c6a3face67961f955d64bb74"><code>aac4328</code></a>
chore: add git attributes file</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/6a33516b8cb87a04a2e208c7a794e58f40f6f9b9"><code>6a33516</code></a>
docs: tweak</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/cff604e90bc562dee16a32efbe6b4471d3e64105"><code>cff604e</code></a>
feat: Python 3.13 support (<a
href="https://github.com/gorakhargosh/watchdog/issues/1052">#1052</a>)</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/7503d34c1f49a96c7ab31b177f6f3a3ffc8ab049"><code>7503d34</code></a>
fix: possible race condition in <code>AutoRestartTrick</code> (<a
href="https://github.com/gorakhargosh/watchdog/issues/1002">#1002</a>)</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/7d4a369e82eea79ef281febb03d9267805f746bb"><code>7d4a369</code></a>
[core] Run ruff, apply several fixes (<a
href="https://github.com/gorakhargosh/watchdog/issues/1033">#1033</a>)</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/7cd723ac433781002062fdf6f6400043f0d9d55d"><code>7cd723a</code></a>
chore: partly move settings from <code>setup.cfg</code> to
<code>pyproject.toml</code></li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/654707e7e640d5b1d15e3714ba4ce54a3597bce9"><code>654707e</code></a>
fix: remove execution rights from <code>events.py</code>, and
<code>watchmedo.py</code>, files</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/4043ef098214732662950c06c2b39407804b3153"><code>4043ef0</code></a>
tests: improve flakyness + clean-up</li>
<li><a
href="https://github.com/gorakhargosh/watchdog/commit/206843c8bc22210c6bd95537fc28d87c552d03d0"><code>206843c</code></a>
chore: remove useless kwarg on <code>BaseObserver</code> subclasses</li>
<li>Additional commits viewable in <a
href="https://github.com/gorakhargosh/watchdog/compare/v4.0.1...v4.0.2">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=watchdog&package-manager=pip&previous-version=4.0.1&new-version=4.0.2)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants