Assert RequestCleanup ran only once

Prevent the RequestCleanup.run() to be called more
than once.
Helps preventing bugs in the request context cleanup
process and allow making changes to the Git/HTTP and
Git/SSH protocols validating the correct request context
management.

Change-Id: I432d36f591ee9015856ac18ad27e98dcdca8e465
This commit is contained in:
Luca Milanesio
2019-12-13 20:21:23 +00:00
parent d2b1208214
commit a89aefab27

View File

@@ -31,9 +31,7 @@ public class RequestCleanup implements Runnable {
/** Register a task to be completed after the request ends. */
public void add(Runnable task) {
synchronized (cleanup) {
if (ran) {
throw new IllegalStateException("Request has already been cleaned up");
}
assertNotRan();
cleanup.add(task);
}
}
@@ -41,6 +39,7 @@ public class RequestCleanup implements Runnable {
@Override
public void run() {
synchronized (cleanup) {
assertNotRan();
ran = true;
for (Iterator<Runnable> i = cleanup.iterator(); i.hasNext(); ) {
try {
@@ -52,4 +51,10 @@ public class RequestCleanup implements Runnable {
}
}
}
private void assertNotRan() {
if (ran) {
throw new IllegalStateException("Request has already been cleaned up");
}
}
}