summaryrefslogtreecommitdiffstats
path: root/inc/taskmanager.inc.php
diff options
context:
space:
mode:
authorSimon Rettberg2020-02-25 16:02:34 +0100
committerSimon Rettberg2020-02-25 16:02:34 +0100
commit3f084dd48ffac58fa3d1563c1afa4f9ae84c4474 (patch)
tree7de2db7ec485ca0b0fc8bb38c0f8cfab2e3f101e /inc/taskmanager.inc.php
parent[permissionmanager] Properly reset builtin roles on upgrade (diff)
downloadslx-admin-3f084dd48ffac58fa3d1563c1afa4f9ae84c4474.tar.gz
slx-admin-3f084dd48ffac58fa3d1563c1afa4f9ae84c4474.tar.xz
slx-admin-3f084dd48ffac58fa3d1563c1afa4f9ae84c4474.zip
[inc/Taskmanager] Reset connection on error
Diffstat (limited to 'inc/taskmanager.inc.php')
-rw-r--r--inc/taskmanager.inc.php25
1 files changed, 23 insertions, 2 deletions
diff --git a/inc/taskmanager.inc.php b/inc/taskmanager.inc.php
index c8ea9d0c..85d5ee39 100644
--- a/inc/taskmanager.inc.php
+++ b/inc/taskmanager.inc.php
@@ -36,7 +36,10 @@ class Taskmanager
{
$len = strlen($message);
$sent = socket_send(self::$sock, pack('N', $len) . $message, $len + 4, 0);
- return $sent === $len + 4;
+ if ($sent === $len + 4)
+ return true;
+ self::reset();
+ return false;
}
/**
@@ -255,11 +258,13 @@ class Taskmanager
while (($bytes = socket_recv(self::$sock, $buf, 4, MSG_WAITALL)) !== false) {
if ($bytes !== 4) {
error_log('TM: Short read');
+ self::reset();
return false;
}
$len = unpack('Nx', $buf)['x'];
if ($len < 0 || $len > 1024 * 1024) {
error_log('TM: Invalid payload length: ' . $len);
+ self::reset();
return false;
}
$message = '';
@@ -268,10 +273,12 @@ class Taskmanager
$ret = socket_recv(self::$sock, $buf, $len, 0);
if ($ret === false) {
error_log('TM: Error reading payload');
+ self::reset();
return false;
}
if ($ret <= 0) {
error_log('TM: Taskmanager closed connection');
+ self::reset();
return false;
}
$message .= $buf;
@@ -307,11 +314,25 @@ class Taskmanager
if (++$tries > 10)
return false;
}
- error_log('Reading taskmanager reply failed, socket error ' . socket_last_error());
+ error_log('TM: Reading reply failed, socket error ' . socket_last_error());
return false;
}
/**
+ * Closes connection and resets the variable.
+ * Should be called if something goes wrong when
+ * sending or receiving and the send or receive
+ * buffer might be in an undefined state.
+ */
+ private static function reset()
+ {
+ if (self::$sock === false)
+ return;
+ socket_close(self::$sock);
+ self::$sock = false;
+ }
+
+ /**
* @param float $deadline end time
*/
private static function updateRecvTimeout($deadline)