From 6822c833be9c9a19cd6c2f43d3dc001bdf1f64cb Mon Sep 17 00:00:00 2001 From: df Date: Sun, 18 Oct 2020 14:53:23 +0000 Subject: [PATCH 1/5] Retry access in case the settings database is locked Create `settings::_safer_query` class proc to allow 5 tries with 1s delay; use it for `_nval_setting` and `_tval_setting` methods --- webif/lib/settings.class | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/webif/lib/settings.class b/webif/lib/settings.class index 05361dd..4251fa6 100644 --- a/webif/lib/settings.class +++ b/webif/lib/settings.class @@ -54,6 +54,21 @@ class settings { rtschedule 0 } +proc {settings _safer_query} { queryText args } { + global settingsdb + # allow 5 (too many?) tries with 1s (too long?) delay to get access + for {set lockCnt 5} {} {sleep 1} { + try { + $settingsdb query $queryText {*}$args + } on error {msg opts} { + if {[string first "database is locked" $msg] >= 0 && [incr lockCnt -1] > 0} { + continue + } + return {*}$opts $msg + } + } +} + settings method hostname {{name ""}} { if {$name == ""} { # Get @@ -104,11 +119,10 @@ settings method smtp_server {{server ""}} { } settings method _nval_setting {name {val -1}} { - global settingsdb if {$val == -1} { # Get - set res [$settingsdb query " + set res [settings::_safer_query " select nval from settings where name = '$name' "] @@ -118,7 +132,7 @@ settings method _nval_setting {name {val -1}} { return 0 } else { # Set - $settingsdb query " + settings::_safer_query " replace into settings(name,nval) values('$name', $val) " @@ -127,11 +141,10 @@ settings method _nval_setting {name {val -1}} { } settings method _tval_setting {name {val -1} {def 0}} { - global settingsdb if {$val eq "-1"} { # Get - set res [$settingsdb query " + set res [settings::_safer_query " select tval from settings where name = '$name' "] @@ -141,7 +154,7 @@ settings method _tval_setting {name {val -1} {def 0}} { return $def } else { # Set - $settingsdb query " + settings::_safer_query " replace into settings(name,tval) values('$name', '%s') " $val From 9b64128c92198764295344e8a48f50d8caf7bfca Mon Sep 17 00:00:00 2001 From: df Date: Sun, 18 Oct 2020 15:48:07 +0000 Subject: [PATCH 2/5] Retry access in case the settings database is locked --- webif/lib/settings.class | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/webif/lib/settings.class b/webif/lib/settings.class index 4251fa6..5b56185 100644 --- a/webif/lib/settings.class +++ b/webif/lib/settings.class @@ -56,10 +56,11 @@ class settings { proc {settings _safer_query} { queryText args } { global settingsdb + # allow 5 (too many?) tries with 1s (too long?) delay to get access - for {set lockCnt 5} {} {sleep 1} { + for {set lockCnt 5} {true} {sleep 1} { try { - $settingsdb query $queryText {*}$args + return [$settingsdb query $queryText {*}$arg] } on error {msg opts} { if {[string first "database is locked" $msg] >= 0 && [incr lockCnt -1] > 0} { continue @@ -122,7 +123,7 @@ settings method _nval_setting {name {val -1}} { if {$val == -1} { # Get - set res [settings::_safer_query " + set res [settings _safer_query " select nval from settings where name = '$name' "] @@ -132,7 +133,7 @@ settings method _nval_setting {name {val -1}} { return 0 } else { # Set - settings::_safer_query " + settings _safer_query " replace into settings(name,nval) values('$name', $val) " @@ -144,7 +145,7 @@ settings method _tval_setting {name {val -1} {def 0}} { if {$val eq "-1"} { # Get - set res [settings::_safer_query " + set res [settings _safer_query " select tval from settings where name = '$name' "] @@ -154,7 +155,7 @@ settings method _tval_setting {name {val -1} {def 0}} { return $def } else { # Set - settings::_safer_query " + settings _safer_query " replace into settings(name,tval) values('$name', '%s') " $val From 7bb66de5f2b54fb17bbf1aaaee41e00fcef2bb42 Mon Sep 17 00:00:00 2001 From: df Date: Sun, 18 Oct 2020 19:19:26 +0000 Subject: [PATCH 3/5] Raise the "database locked" error from caller context --- webif/lib/settings.class | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/webif/lib/settings.class b/webif/lib/settings.class index 5b56185..fa74b11 100644 --- a/webif/lib/settings.class +++ b/webif/lib/settings.class @@ -62,8 +62,14 @@ proc {settings _safer_query} { queryText args } { try { return [$settingsdb query $queryText {*}$arg] } on error {msg opts} { - if {[string first "database is locked" $msg] >= 0 && [incr lockCnt -1] > 0} { - continue + if {[string first "database is locked" $msg] >= 0} { + if {[incr lockCnt -1] > 0} { + continue + } else { + # raise error from caller + set msg "Persistently unable to access Settings: database locked" + incr opts(-level) + } } return {*}$opts $msg } From 3e9ecc8a7ceab41ce383114b9c3d856251391a98 Mon Sep 17 00:00:00 2001 From: df Date: Mon, 19 Oct 2020 11:44:53 +0000 Subject: [PATCH 4/5] Change to 0.5s, doubling for 4 tries --- webif/lib/settings.class | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webif/lib/settings.class b/webif/lib/settings.class index fa74b11..ac7c6d5 100644 --- a/webif/lib/settings.class +++ b/webif/lib/settings.class @@ -57,8 +57,8 @@ class settings { proc {settings _safer_query} { queryText args } { global settingsdb - # allow 5 (too many?) tries with 1s (too long?) delay to get access - for {set lockCnt 5} {true} {sleep 1} { + # allow 4 tries from 0.5s delay, doubling, to get access + for {set lockCnt 4; set delay 0.5} {true} {sleep $delay; set delay $(2*$delay)} { try { return [$settingsdb query $queryText {*}$arg] } on error {msg opts} { From 7c600be77e3aa04123cab25e1453c2eab980d496 Mon Sep 17 00:00:00 2001 From: df Date: Tue, 20 Oct 2020 18:34:19 +0000 Subject: [PATCH 5/5] Retry access in case the settings database is locked --- webif/lib/settings.class | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webif/lib/settings.class b/webif/lib/settings.class index ac7c6d5..f5edda2 100644 --- a/webif/lib/settings.class +++ b/webif/lib/settings.class @@ -60,7 +60,7 @@ proc {settings _safer_query} { queryText args } { # allow 4 tries from 0.5s delay, doubling, to get access for {set lockCnt 4; set delay 0.5} {true} {sleep $delay; set delay $(2*$delay)} { try { - return [$settingsdb query $queryText {*}$arg] + return [$settingsdb query $queryText {*}$args] } on error {msg opts} { if {[string first "database is locked" $msg] >= 0} { if {[incr lockCnt -1] > 0} {