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

Fix Windows Ppid Cache Race Condition #962

Merged
merged 1 commit into from
Oct 29, 2020
Merged

Fix Windows Ppid Cache Race Condition #962

merged 1 commit into from
Oct 29, 2020

Conversation

AtakanColak
Copy link
Contributor

As per #961, a RWMutex to Process struct and thread-safe ppid access functions are added which are now used for windows but virtually can be used for other OSes in the future.

Another solution would be to not cache at all, but this solution reduces overall system calls and in windows it makes quite the difference.

Please do review. @Lomanic @shirou

@AtakanColak
Copy link
Contributor Author

go vet still fails, looking into it

@AtakanColak
Copy link
Contributor Author

go vet check passes after changing the mutex to a pointer that is initialized with NewProcess call

@AtakanColak
Copy link
Contributor Author

Squashed commits, waiting for review.

Copy link
Collaborator

@Lomanic Lomanic left a comment

Choose a reason for hiding this comment

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

Thanks for PR, I didn't yet have a chance to test it. See my comment.

process/process.go Outdated Show resolved Hide resolved
@shirou
Copy link
Owner

shirou commented Oct 18, 2020

Sorry, #966 makes conflicts. But If you follow the comment and make the changes, this conflicts may resolved,

@AtakanColak
Copy link
Contributor Author

@Lomanic thank you for the doc link, seeing a caching in windows ppid showed me that I was right to start this feature beforehand. Resolved it by carrying the methods to process_windows.go

@shirou resolved the conflicts.

Even though my tests passed, please do test before merge.

@AtakanColak AtakanColak requested a review from Lomanic October 21, 2020 12:02
@shirou
Copy link
Owner

shirou commented Oct 21, 2020

Thank you so much! I can reproduce race condition by using this test and this PR can fix.

% go test --race -v -run Test_Process_Ppid
func Test_Process_Ppid(t *testing.T) {
	wg := sync.WaitGroup{}
	testCount := 10
	p := testGetProcess()
	wg.Add(testCount)
	for i := 0; i < testCount; i++ {
		go func(j int) {
			ppid, err := p.Ppid()
			wg.Done()
			skipIfNotImplementedErr(t, err)
			if err != nil {
				t.Errorf("Ppid() failed, %v", err)
			}

			if j == 9 {
				t.Logf("Ppid(): %d", ppid)
			}
		}(i)
	}
	wg.Wait()
}

Could you replace the current Test_Process_Ppid() by this code, confirm, and commit again? Thanks!

@AtakanColak
Copy link
Contributor Author

@shirou test passed on my windows. Added to the commit.

@Lomanic
Copy link
Collaborator

Lomanic commented Oct 21, 2020

Problem with your test @shirou is that it doesn't test the same thing anymore (getting a non-zero value, and the same value as os.Getppid()). I would restore the previous one and put that one new test to a dedicated test like in https://github.com/shirou/gopsutil/blob/master/disk/disk_test.go#L70-L85.

And I would also suggest to be explicit that this test is made to detect race conditions (in its name). This test could even be put in a new process/process_race_test.go with // +build race flag on top so it only runs when the race detector is enabled.

@shirou
Copy link
Owner

shirou commented Oct 22, 2020

Ah, yes, totally agree to @Lomanic .

I'm really sorry but could you revert that commit and create a new process/process_race_test.go file? > @AtakanColak .

@AtakanColak
Copy link
Contributor Author

@Lomanic @shirou committed with a process_race_test.go instead.

@AtakanColak
Copy link
Contributor Author

@Lomanic could you review and merge if deemed sufficient please.

@Lomanic Lomanic merged commit f810d51 into shirou:master Oct 29, 2020
@AtakanColak AtakanColak deleted the fix-ppid-race-961 branch October 30, 2020 07:55
shirou added a commit that referenced this pull request Oct 31, 2020
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