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

To support Virtual Thread, replace synchronized with ReentrantLock. #116

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.amazonaws.secretsmanager.caching.cache;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.locks.ReentrantLock;

import com.amazonaws.secretsmanager.caching.SecretCacheConfiguration;

Expand Down Expand Up @@ -48,7 +49,7 @@ public abstract class SecretCacheObject<T> {
protected final String secretId;

/** A private object to synchronize access to certain methods. */
protected final Object lock = new Object();
private final ReentrantLock lock = new ReentrantLock();

/** The AWS Secrets Manager client to use for requesting secrets. */
protected final SecretsManagerClient client;
Expand Down Expand Up @@ -229,9 +230,12 @@ public boolean refreshNow() throws InterruptedException {
Thread.sleep(sleep);
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't had the opportunity to explore how Virtual Threads work, does the regular Thread class sleep() call still work the same way with them?

Copy link
Author

Choose a reason for hiding this comment

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

The regualar Thread.sleep() still works here. I tested it and didn't find problem.
The problem only occurs when the synchronized block has blocking operation.
You can reproduce the issue in Linux environment with the code in #115.


// Perform the requested refresh
synchronized (lock) {
lock.lock();
try {
refresh();
return (null == this.exception);
} finally {
lock.unlock();
}
}

Expand All @@ -241,13 +245,16 @@ public boolean refreshNow() throws InterruptedException {
* @return The cached GetSecretValue result.
*/
public GetSecretValueResponse getSecretValue() {
synchronized (lock) {
lock.lock();
try {
refresh();
if (null == this.data) {
if (null != this.exception) { throw this.exception; }
}

return this.getSecretValue(this.getResult());
} finally {
lock.unlock();
}
}

Expand Down
Loading