Skip to content

memory: fix deadlock in operations.Purge - #7670

Merged
nielash merged 1 commit into
rclone:masterfrom
nielash:fix-memory-deadlock
Mar 27, 2024
Merged

nielash merged 1 commit into
rclone:masterfrom
nielash:fix-memory-deadlock

Conversation

@nielash

@nielash nielash commented Mar 12, 2024

Copy link
Copy Markdown
Contributor

What is the purpose of this change?

Before this change, the Memory backend had the potential to deadlock under certain conditions, if the ListR callback required locking the b.mu mutex. This was the case with operations.Purge, because Memory has no Purge method, and the fallback option does:

err = DeleteFiles(ctx, listToChan(ctx, f, dir))

which potentially starts removing objects before the listing has completed.

This change fixes the issue by ensuring the mu is unlocked for the duration of fn.

Was the change discussed in an issue or in the forum before?

No.

Checklist

  • I have read the contribution guidelines.
  • I have added tests for all changes in this PR if appropriate.
  • I have added documentation for the changes if appropriate.
  • All commit messages are in house style.
  • I'm done, this Pull Request is ready for review :-)

@nielash nielash added this to the v1.67 milestone Mar 12, 2024
@nielash
nielash marked this pull request as ready for review March 12, 2024 00:59

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is very tricky to solve properly!

The problem is that ListR calls out of the backend with the lock held. We can avoid that by accumulating all the entries in memory first (which is a bit wasteful on memory) before calling the function that is passed into ListR with the entries in one go at the end.

Comment thread backend/memory/memory.go Resolved
Comment thread fs/operations/operations_test.go Outdated

@ncw ncw Mar 13, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This test should probably be in the internal tests for the memory backend as creating a lot of files will slow down all the integration tests!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My thought was to check whether any other backends have this same problem (now or in the future) -- although that seems unlikely as this is somewhat specific to memory.

Maybe there's a more efficient way to check it? The reason for creating so many files is that this is basically a race condition which would not occur with fewer files.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As you've probably noticed I try not to get the integration tests to do too much work! Some backends take several seconds to upload a file so uploading 100 files may increase the run time by 5 minutes for the slow backends.

I think this test is fine, but let's put it in the Internal tests for the memory backend as I think it is unlikely that other backends will have this problem in the future.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok. Full disclosure: bisync's volatile test does something very similar (uploading 100 files) which is how I found this issue originally. Should I put in a condition to skip that test on non-local?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Probably a good idea, yes!

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a tricky problem to solve!

I think getting ListR to batch all the entries before calling the callback on them is probably the best solution. Not particularly nice as it uses more memory than it has to but OK...!

@nielash

nielash commented Mar 13, 2024

Copy link
Copy Markdown
Contributor Author

This is a tricky problem to solve!

I think getting ListR to batch all the entries before calling the callback on them is probably the best solution.

I see. Is that better than just removing ListR and falling back to List directory traversal when necessary?

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code looks great now - comments about the test inline.

I don't want to remove ListR for memory as it stands in for a bucket based remote in the tests and they all implement ListR. If this was a normal backend then yes removing it would make perfect sense, but I view it more as an aid to testing.

Thank you :-)

Comment thread fs/operations/operations_test.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As you've probably noticed I try not to get the integration tests to do too much work! Some backends take several seconds to upload a file so uploading 100 files may increase the run time by 5 minutes for the slow backends.

I think this test is fine, but let's put it in the Internal tests for the memory backend as I think it is unlikely that other backends will have this problem in the future.

Before this change, the Memory backend had the potential to deadlock under
certain conditions, if the ListR callback required locking the b.mu mutex. This
was the case with operations.Purge, because Memory has no Purge method, and the
fallback option does:

	err = DeleteFiles(ctx, listToChan(ctx, f, dir))

which potentially starts removing objects before the listing has completed.

This change fixes the issue by batching all the entries before calling the
callback on them.
@nielash
nielash force-pushed the fix-memory-deadlock branch from 41e4599 to 5b630ca Compare March 27, 2024 01:45
nielash added a commit to nielash/rclone that referenced this pull request Mar 27, 2024
This change officially adds bisync to the nightly integration tests for all
backends.

This will be part of giving us the confidence to take bisync out of beta.

A number of fixes have been added to account for features which can differ on
different backends -- for example, hash types / modtime support, empty
directories, unicode normalization, and unimportant differences in log output.
We will likely find that more of these are needed once we start running these
with the full set of remotes.

Additionally, bisync's extremely sensitive tests revealed a few bugs in other
backends that weren't previously covered by other tests. Fixes for those issues
have been submitted on the following separate PRs (and bisync test failures will
be expected until they are merged):

- rclone#7670 memory: fix deadlock in operations.Purge
- rclone#7688 memory: fix incorrect list entries when rooted at subdirectory
- rclone#7690 memory: fix dst mutating src after server-side copy
- rclone#7692 dropbox: fix chunked uploads when size <= chunkSize

Relatedly, workarounds have been put in place for the following backend
limitations that are unsolvable for the time being:

- rclone#3262 drive is sometimes aware of trashed files/folders when it shouldn't be
- rclone#6199 dropbox can't handle emojis and certain other characters
- rclone#4590 onedrive API has longstanding bug for conflictBehavior=replace in
	server-side copy/move
nielash added a commit that referenced this pull request Mar 27, 2024
This change officially adds bisync to the nightly integration tests for all
backends.

This will be part of giving us the confidence to take bisync out of beta.

A number of fixes have been added to account for features which can differ on
different backends -- for example, hash types / modtime support, empty
directories, unicode normalization, and unimportant differences in log output.
We will likely find that more of these are needed once we start running these
with the full set of remotes.

Additionally, bisync's extremely sensitive tests revealed a few bugs in other
backends that weren't previously covered by other tests. Fixes for those issues
have been submitted on the following separate PRs (and bisync test failures will
be expected until they are merged):

- #7670 memory: fix deadlock in operations.Purge
- #7688 memory: fix incorrect list entries when rooted at subdirectory
- #7690 memory: fix dst mutating src after server-side copy
- #7692 dropbox: fix chunked uploads when size <= chunkSize

Relatedly, workarounds have been put in place for the following backend
limitations that are unsolvable for the time being:

- #3262 drive is sometimes aware of trashed files/folders when it shouldn't be
- #6199 dropbox can't handle emojis and certain other characters
- #4590 onedrive API has longstanding bug for conflictBehavior=replace in
	server-side copy/move

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you - please merge :-)

@nielash
nielash merged commit 2b0a25a into rclone:master Mar 27, 2024
lafkpages pushed a commit to lafkpages/rclone that referenced this pull request Jul 20, 2026
This change officially adds bisync to the nightly integration tests for all
backends.

This will be part of giving us the confidence to take bisync out of beta.

A number of fixes have been added to account for features which can differ on
different backends -- for example, hash types / modtime support, empty
directories, unicode normalization, and unimportant differences in log output.
We will likely find that more of these are needed once we start running these
with the full set of remotes.

Additionally, bisync's extremely sensitive tests revealed a few bugs in other
backends that weren't previously covered by other tests. Fixes for those issues
have been submitted on the following separate PRs (and bisync test failures will
be expected until they are merged):

- rclone#7670 memory: fix deadlock in operations.Purge
- rclone#7688 memory: fix incorrect list entries when rooted at subdirectory
- rclone#7690 memory: fix dst mutating src after server-side copy
- rclone#7692 dropbox: fix chunked uploads when size <= chunkSize

Relatedly, workarounds have been put in place for the following backend
limitations that are unsolvable for the time being:

- rclone#3262 drive is sometimes aware of trashed files/folders when it shouldn't be
- rclone#6199 dropbox can't handle emojis and certain other characters
- rclone#4590 onedrive API has longstanding bug for conflictBehavior=replace in
	server-side copy/move
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants