Put timeout on read lock used by web server - make sure it can't be indefinite
This commit is contained in:
parent
1fd933e07f
commit
3aad863d27
2 changed files with 19 additions and 2 deletions
|
|
@ -63,9 +63,16 @@ public class FileLockManager {
|
||||||
* Get read lock on file - multiple readers allowed, blocks writers
|
* Get read lock on file - multiple readers allowed, blocks writers
|
||||||
*/
|
*/
|
||||||
public static boolean getReadLock(File f) {
|
public static boolean getReadLock(File f) {
|
||||||
|
return getReadLock(f, -1);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get read lock on file - multiple readers allowed, blocks writers - with timeout (msec)
|
||||||
|
*/
|
||||||
|
public static boolean getReadLock(File f, long timeout) {
|
||||||
String fn = f.getPath();
|
String fn = f.getPath();
|
||||||
synchronized(lock) {
|
synchronized(lock) {
|
||||||
boolean got_lock = false;
|
boolean got_lock = false;
|
||||||
|
boolean first_wait = true;
|
||||||
while(!got_lock) {
|
while(!got_lock) {
|
||||||
Integer lockcnt = filelocks.get(fn); /* Get lock count */
|
Integer lockcnt = filelocks.get(fn); /* Get lock count */
|
||||||
if(lockcnt == null) {
|
if(lockcnt == null) {
|
||||||
|
|
@ -78,7 +85,14 @@ public class FileLockManager {
|
||||||
}
|
}
|
||||||
else { /* Write lock in place */
|
else { /* Write lock in place */
|
||||||
try {
|
try {
|
||||||
lock.wait();
|
if((timeout > 0) && (!first_wait)) { /* We already waited */
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(timeout < 0)
|
||||||
|
lock.wait();
|
||||||
|
else
|
||||||
|
lock.wait(timeout);
|
||||||
|
first_wait = false;
|
||||||
} catch (InterruptedException ix) {
|
} catch (InterruptedException ix) {
|
||||||
Log.severe("getReadLock(" + fn + ") interrupted");
|
Log.severe("getReadLock(" + fn + ") interrupted");
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,10 @@ public class FilesystemHandler extends FileHandler {
|
||||||
@Override
|
@Override
|
||||||
protected InputStream getFileInput(String path, HttpRequest request, HttpResponse response) {
|
protected InputStream getFileInput(String path, HttpRequest request, HttpResponse response) {
|
||||||
File file = new File(root, path);
|
File file = new File(root, path);
|
||||||
FileLockManager.getReadLock(file);
|
if(!FileLockManager.getReadLock(file, 5000)) { /* Wait up to 5 seconds for lock */
|
||||||
|
Log.severe("Timeout waiting for lock on file " + file.getPath());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
FileInputStream result = null;
|
FileInputStream result = null;
|
||||||
try {
|
try {
|
||||||
if (file.getCanonicalPath().startsWith(root.getAbsolutePath()) && file.isFile()) {
|
if (file.getCanonicalPath().startsWith(root.getAbsolutePath()) && file.isFile()) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue