After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 135452 - "Failed to lock" -> recent files disappear
"Failed to lock" -> recent files disappear
Status: VERIFIED INCOMPLETE
Product: gedit
Classification: Applications
Component: general
2.5.x
Other Linux
: Normal normal
: ---
Assigned To: Gedit maintainers
gedit QA volunteers
: 143821 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2004-02-26 04:55 UTC by Rodd Clarkson
Modified: 2009-08-15 18:40 UTC
See Also:
GNOME target: ---
GNOME version: 2.5/2.6



Description Rodd Clarkson 2004-02-26 04:55:57 UTC
I'm getting a lot of the following error message on the console.

** (gedit:22996): WARNING **: Failed to lock:  Resource temporarily unavailable
Comment 1 Paolo Maggi 2004-03-03 13:59:05 UTC
This is (was?) due to egg-recent
I think it should fixed in the latest gnome 2.5 release.
Could you please confirm?
Comment 2 Paolo Maggi 2004-03-27 17:13:05 UTC
snorp: is this fixed?
Comment 3 James Willcox 2004-03-27 18:13:30 UTC
Yeah, I haven't seen this in quite some time....
Comment 4 Rodd Clarkson 2004-03-28 23:32:50 UTC
I have!  It still quite common for me.
Comment 5 Rodd Clarkson 2004-03-29 01:46:27 UTC
It might be worth noting that most of the editing I do with gedit is on files in
a NFS share.
Comment 6 James Willcox 2004-03-29 20:22:40 UTC
Ah, ok.  File locking doesn't always work on NFS.  You can (mostly) ignore the
message.
Comment 7 Rodd Clarkson 2004-03-31 00:34:50 UTC
So, if the error message can be ignored, maybe it shouldn't be printed?
Comment 8 Paolo Borelli 2004-07-13 13:55:29 UTC
*** Bug 143821 has been marked as a duplicate of this bug. ***
Comment 9 Paolo Borelli 2004-07-13 13:57:35 UTC
This still happens on and off in 2.6...

From bug 143821: the problem also causes the recent files list to disappear
Comment 10 Paolo Maggi 2004-07-14 16:05:51 UTC
It is still quite common for me (and I don't use NFS).

I have written a simple patch to try to fix this problem, but I cannot
fully test it due to a crash bug in my build.

snorp: pbor: could you please try to give a look at my patch?
I'm not sure about the select (i.e. is it really waiting for a max interval
of 1 second? Or is it 10 seconds?)


Index: gedit/recent-files/egg-recent-model.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/recent-files/egg-recent-model.c,v
retrieving revision 1.13
diff -u -p -r1.13 egg-recent-model.c
--- gedit/recent-files/egg-recent-model.c	13 Feb 2004 18:06:26 -0000	1.13
+++ gedit/recent-files/egg-recent-model.c	14 Jul 2004 16:00:36 -0000
@@ -875,27 +875,38 @@ static gboolean
 egg_recent_model_lock_file (FILE *file)
 {
 	int fd;
-	gboolean locked = FALSE;
-	gint	try = 4;
+	gint	try = 5;
 
 	rewind (file);
 	fd = fileno (file);
 
-	/* Attempt to lock the file 4 times,
-	 * waiting 1 second in between attempts.
+	/* Attempt to lock the file 5 times,
+	 * waiting a random interval (< 1 second) 
+	 * in between attempts.
 	 * We should really be doing asynchronous
 	 * locking, but requires substantially larger
 	 * changes.
 	 */
 	
-	while (try-- > 0)
+	while (try > 0)
 	{
-		if ((locked = lockf (fd, F_TLOCK, 0) < 0 ? FALSE : TRUE));
-			break;
-		sleep (1);
+		int rand_interval;
+		struct timeval tv;
+
+		if (lockf (fd, F_TLOCK, 0) == 0)
+			return TRUE;
+
+		rand_interval = 1 + (int) (10.0 * rand()/(RAND_MAX + 1.0));
+			 
+		tv.tv_sec = 0;
+           	tv.tv_usec = 100000 * rand_interval;  
+
+		select (0, NULL, NULL, NULL, &tv);
+
+		--try;
 	}
 
-	return locked;
+	return FALSE;
 }
 
 static gboolean
@@ -906,7 +917,7 @@ egg_recent_model_unlock_file (FILE *file
 	rewind (file);
 	fd = fileno (file);
 
-	return lockf (fd, F_ULOCK, 0) < 0 ? FALSE : TRUE;
+	return (lockf (fd, F_ULOCK, 0) == 0) ? TRUE : FALSE;
 }
 
 static void
Comment 11 James Willcox 2004-07-14 16:12:02 UTC
This patch looks good to me.  I don't get the file locking problem described, so
I'm not sure if it really fixes it.  Really, we should do asynchronous locking
(does that exist?)
Comment 12 Paolo Maggi 2004-07-14 16:26:31 UTC
ok, thanks.

Dummo about asynchronous locking (the commend was in your code :-)
Comment 13 Paolo Borelli 2004-07-14 17:44:17 UTC
While taking a look at the problem I noticed a behavior which seems fairly
reproduceable (it happened 4/4)

1) open a file in gedit -> recent files list is ok
2) make some modification and save -> recent files list disappear

If the bug is triggered by the file write, it would explain why eog and gpdf
(which only read the files) do not suffer this problem
Comment 14 Paolo Maggi 2004-07-19 15:09:34 UTC
pbor: it seems to work well with my local patch.
Could you please test it and try to reproduce it?

I'm attaching a new version of my patch using g_usleep instead of select.

Index: gedit/recent-files/egg-recent-model.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/recent-files/egg-recent-model.c,v
retrieving revision 1.13
diff -u -p -r1.13 egg-recent-model.c
--- gedit/recent-files/egg-recent-model.c	13 Feb 2004 18:06:26 -0000	1.13
+++ gedit/recent-files/egg-recent-model.c	19 Jul 2004 15:08:35 -0000
@@ -875,27 +875,34 @@ static gboolean
 egg_recent_model_lock_file (FILE *file)
 {
 	int fd;
-	gboolean locked = FALSE;
-	gint	try = 4;
+	gint	try = 5;
 
 	rewind (file);
 	fd = fileno (file);
 
-	/* Attempt to lock the file 4 times,
-	 * waiting 1 second in between attempts.
+	/* Attempt to lock the file 5 times,
+	 * waiting a random interval (< 1 second) 
+	 * in between attempts.
 	 * We should really be doing asynchronous
 	 * locking, but requires substantially larger
 	 * changes.
 	 */
 	
-	while (try-- > 0)
+	while (try > 0)
 	{
-		if ((locked = lockf (fd, F_TLOCK, 0) < 0 ? FALSE : TRUE));
-			break;
-		sleep (1);
+		int rand_interval;
+
+		if (lockf (fd, F_TLOCK, 0) == 0)
+			return TRUE;
+
+		rand_interval = 1 + (int) (10.0 * rand()/(RAND_MAX + 1.0));
+			 
+           	g_usleep (100000 * rand_interval);
+
+		--try;
 	}
 
-	return locked;
+	return FALSE;
 }
 
 static gboolean
@@ -906,7 +913,7 @@ egg_recent_model_unlock_file (FILE *file
 	rewind (file);
 	fd = fileno (file);
 
-	return lockf (fd, F_ULOCK, 0) < 0 ? FALSE : TRUE;
+	return (lockf (fd, F_ULOCK, 0) == 0) ? TRUE : FALSE;
 }
 
 static void
Comment 15 Paolo Borelli 2004-07-19 15:50:08 UTC
With this patch I cannot trigger it anymore and the patch makes sense on its
own. This is good.

Nontheless I am wondering if someone with knowledge of recent-files code could
make sense of the info which relates this bug to the saving operation (see my
last comment), provided that someone else manage to reproduce it following my
instructions.

Since the patch seems unrelated, I fear that maybe we are just papering over the
bug.
Comment 16 Paolo Maggi 2004-07-19 16:02:36 UTC
Ok, I'm going to commit the patch.

snorp: do you think it should be committed to libegg too?
Comment 17 James Willcox 2004-07-19 16:08:21 UTC
Yeah, it looks good.  Please commit to libegg :)
Comment 18 Paolo Maggi 2004-07-19 16:25:29 UTC
Committed to libegg too.