summaryrefslogtreecommitdiff
path: root/daemon
diff options
context:
space:
mode:
authorBenjamin Otte <otte@gnome.org>2009-09-03 19:05:25 +0200
committerBenjamin Otte <otte@gnome.org>2009-09-03 19:13:20 +0200
commitf1c2079b78c8424b83d758f88bfe16027a415b42 (patch)
tree08740b0e297bb5c8a2fdf26593d75cef6a56274b /daemon
parentcc768c9f998b0d7700f6dbaf9298489e0d85f4e9 (diff)
downloadgvfs-f1c2079b78c8424b83d758f88bfe16027a415b42.tar.gz
Make this code more robust against quickly opening connections
- Don't assume a new connection failed when it was cancelled - Only update max_connections when we're the only thread that opened a new connection.
Diffstat (limited to 'daemon')
-rw-r--r--daemon/gvfsftptask.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/daemon/gvfsftptask.c b/daemon/gvfsftptask.c
index d236ef8a..4cabf147 100644
--- a/daemon/gvfsftptask.c
+++ b/daemon/gvfsftptask.c
@@ -209,6 +209,7 @@ g_vfs_ftp_task_acquire_connection (GVfsFtpTask *task)
if (ftp->connections < ftp->max_connections)
{
+ static GThread *last_thread = NULL;
/* Save current number of connections here, so we can limit maximum
* connections later.
* This is necessary for threading reasons (connections can be
@@ -216,6 +217,7 @@ g_vfs_ftp_task_acquire_connection (GVfsFtpTask *task)
guint maybe_max_connections = ftp->connections;
ftp->connections++;
+ last_thread = g_thread_self ();
g_mutex_unlock (ftp->mutex);
task->conn = g_vfs_ftp_connection_new (ftp->addr, task->cancellable, &task->error);
if (G_LIKELY (task->conn != NULL))
@@ -227,19 +229,27 @@ g_vfs_ftp_task_acquire_connection (GVfsFtpTask *task)
break;
}
- g_vfs_ftp_task_clear_error (task);
g_vfs_ftp_connection_free (task->conn);
task->conn = NULL;
g_mutex_lock (ftp->mutex);
ftp->connections--;
- ftp->max_connections = MIN (ftp->max_connections, maybe_max_connections);
- if (ftp->max_connections == 0)
+ /* If this value is still equal to our thread it means there were no races
+ * trying to open connections and the maybe_max_connections value is
+ * reliable. */
+ if (last_thread == g_thread_self () &&
+ !g_vfs_ftp_task_error_matches (task, G_IO_ERROR, G_IO_ERROR_CANCELLED))
{
- g_debug ("no more connections left, exiting...\n");
- /* FIXME: shut down properly */
- exit (0);
+ g_print ("maybe: %u, max %u (due to %s)\n", maybe_max_connections, ftp->max_connections, task->error->message);
+ ftp->max_connections = MIN (ftp->max_connections, maybe_max_connections);
+ if (ftp->max_connections == 0)
+ {
+ g_debug ("no more connections left, exiting...\n");
+ /* FIXME: shut down properly */
+ exit (0);
+ }
}
+ g_vfs_ftp_task_clear_error (task);
continue;
}