Skip to content

fix: data race running many processes concurrently#167

Merged
pepicrft merged 4 commits intotuist:mainfrom
danpalmer:fix-data-race
Jan 22, 2025
Merged

fix: data race running many processes concurrently#167
pepicrft merged 4 commits intotuist:mainfrom
danpalmer:fix-data-race

Conversation

@danpalmer
Copy link
Copy Markdown
Contributor

This PR is an attempt at fixing #160. A test is included which attempts to exercise this case. It typically fails when running before the fix, and appears to pass every time after the fix.

I realise this is a bit of a departure on the code, very open to feedback on this, although I'd ask that you give any non-style changes a go locally, as this took a lot of trial and error to get to and lots of changes that one might expect would work, actually won't.

During development of this I found some other issues where similar things can happen (reading from closed file handles) in the lookupExecutable call, as this isn't as strict as the main command running. I'll attempt to tackle that separately, and haven't managed to reproduce that in a test yet.

This test is flaky rather than always failing, but with 1000 runs it's sufficiently flaky to be useful.
There are lots of potential races when reading from process file handles. This approach extracts the handling into the file handle itself, which makes it easier to get the lifecycle right.

This also unifies reading from the file handles into one place, as we were losing data between the readabilityHandler and sync readToEnd calls.
@danpalmer
Copy link
Copy Markdown
Contributor Author

Hey @waltflanagan, any thoughts on this?

@waltflanagan
Copy link
Copy Markdown
Contributor

@danpalmer thanks for the tag, i'll try to find some time to take a look over the next day or two. This is tricky stuff, thanks for taking a look.

@waltflanagan
Copy link
Copy Markdown
Contributor

This may also help if you haven't come across it yet. https://developer.apple.com/forums/thread/690310

It does mention needing to dispatch to main in the termination handler to avoid a race in the IO handlers. I wonder if we ended up falling into that.

@pepicrft pepicrft changed the title Fix data race fix: data race running many processes concurrently Jan 22, 2025
@pepicrft
Copy link
Copy Markdown
Contributor

This may also help if you haven't come across it yet. https://developer.apple.com/forums/thread/690310

It does mention needing to dispatch to main in the termination handler to avoid a race in the IO handlers. I wonder if we ended up falling into that.

I didn't know about this one. I'd say let's merge this work, and if the issue still manifests after, we can mimic the implementation suggested in that thread. I have to say it's insane the amount of dancing it's required to get that to work. In other standard libraries, it's much more straightforward.

On another note, there's also subprocess coming to Foundation, so we'll eventually be able to deprecate this work.

I realise this is a bit of a departure on the code, very open to feedback on this, although I'd ask that you give any non-style changes a go locally, as this took a lot of trial and error to get to and lots of changes that one might expect would work, actually won't.

That's completely fine. As long as it does the job respecting the contract of the API, how it's implemented internally is not that important.

During development of this I found some other issues where similar things can happen (reading from closed file handles) in the lookupExecutable call, as this isn't as strict as the main command running. I'll attempt to tackle that separately, and haven't managed to reproduce that in a test yet.

Please, go ahead with those. I'll add you as a collaborator of the repository so that you don't have to work from forks, which is slightly more inconvenient.

import Testing
@testable import Command

struct CommandRunnerRaceTests {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danpalmer I took the opportunity to adjust your test to use Swift Testing instead of XCTest, which we are trying to use less and less.

@pepicrft
Copy link
Copy Markdown
Contributor

@all-contributors add @danpalmer for code

@allcontributors
Copy link
Copy Markdown
Contributor

@pepicrft

I've put up a pull request to add @danpalmer! 🎉

@pepicrft pepicrft merged commit 368039d into tuist:main Jan 22, 2025
@pepicrft
Copy link
Copy Markdown
Contributor

Command 0.11.18 is available with these changes: https://github.com/tuist/Command/releases/tag/0.11.18

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.

3 participants