Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Multi-thread: Infinite loop in trimToSize #81

Open
la-urre opened this issue May 15, 2015 · 5 comments
Open

Multi-thread: Infinite loop in trimToSize #81

la-urre opened this issue May 15, 2015 · 5 comments

Comments

@la-urre
Copy link

la-urre commented May 15, 2015

Hello guys,

I use DiskLruCache in a multi-thread environment. I get an infinite loop in the trimToSize method:

while (size > maxSize) {
Map.Entry<String, Entry> toEvict = lruEntries.entrySet().iterator().next();
remove(toEvict.getKey());
}

The remove call fails to remove the entry, as an other thread is editing the toEvict entry. Shouldn't we check if the remove worked, and for example silently return if it did not ?

@swankjesse
Copy link
Collaborator

Yup, it should definitely skip entries being edited.

la-urre pushed a commit to la-urre/DiskLruCache that referenced this issue May 18, 2015
@la-urre
Copy link
Author

la-urre commented May 18, 2015

Tried a fix, let me know what you think about it.

@swankjesse
Copy link
Collaborator

It's close to what we want. I think it's going to run faster as a single loop that runs until the size is sufficiently small, or there's nothing left to remove. (Since we only allocate one iterator). Even better if you can fast-fail at the front and avoid allocating the iterator at all if the cache is already sufficiently small.

@swankjesse
Copy link
Collaborator

Send a pull request?

@swankjesse
Copy link
Collaborator

And even better, send the pull request against OkHttp? Its DiskLruCache is developed further than this one. Someday we should just simplify this project into a repackaging of a subset of that code.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants