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

Cherry-Pick safe and tested commits from bled112-refactor into master #933

Merged
merged 4 commits into from
Dec 20, 2019

Conversation

timburke
Copy link
Member

@mattrunchey These commits were already reviewed and approved in the feat-bled112-refactor branch last week (or the week before). I'm pulling them out and cherry-picking over to master because they're generally useful apart from that refactor and safe.

I'm working to make feat-bled112-refactor less massive so that we can get smaller PRs into master with backwards compatible improvements without a giant long running feature-branch merge at the end.

The current plan is:

  • Move things that are backwards compatible and safe from feat-bled112-refactor into master (this PR) so that the feature branch becomes smaller.
  • Restructure feat-bled112-refactor to use the code now in iotile-transport-blelib.
  • Update feat-bled112-refactor to not swap out the current device adapter for a new version so it's backwards compatible.
  • Merge feat-bled112-refactor into master since it's now backwards compatible and then start doing smaller feature PRs to complete the transition.

Previously, when stop() was called on an async wrapper on top of
a legacy device adapter, it would result in two calls to stop()
which would stop the underlying serial device twice on the bled112
adapter.

This commits fixes the issue by deferring all actual stop behavior
to the background task that manages the legacy adapter and stopping
that task when stop() is called directly.
There's a CPython bug that causes event loops running in background
threads to raise an exception on close.  The cause is that the
default event loop changed to ProactorEventLoop on windows and there
was a latent bug using a ProactorEventloop in a background thread.

This commit checks for this issue and patches the event loop policy
only on python 3.8.0 and windows to use the old SelectorEventloop.
OperationManager before was basically a POC of how you could create
a class that lets you write coroutines to block on complex network
activity without requiring one thread per blocking operation or
complex callback based code.

The tricky part of writing such coroutines is that you tend to get
race conditions when performing complex operations such as "gather"
operations where you send a command that is acknowledged and then
results in a flurry of events terminated by a "finished" event.

You need to be able to wait for the first ack event but then wait for
an unknown number of events terminated by a known event.  Without
careful planning you have a race condition where you install a filter
to look for events after you receive the acknowledgement packet but
if the first event comes very soon after you'll miss it.

The traditional fix is to have a single thread process packets one
at a time with a single operation in flight to pause packet processing
until you setup your next filters.

OperationManager lets you do that with coroutines so you can have
multiple parallel operations running without having a race in any of
them.
@timburke timburke changed the title Cherry-Pick commits from bled112-refactor into master Cherry-Pick safe and tested commits from bled112-refactor into master Dec 20, 2019
@timburke timburke merged commit 3b4072e into master Dec 20, 2019
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