diff --git a/opendkim/opendkim.c b/opendkim/opendkim.c
--- a/opendkim/opendkim.c
+++ b/opendkim/opendkim.c
@@ -4725,10 +4725,9 @@ dkimf_securefile(const char *path, ino_t *ino, uid_t myuid, char *err,
if (myuid == (uid_t) -1)
myuid = pw->pw_uid;
- pthread_mutex_unlock(&pwdb_lock);
-
#ifdef HAVE_REALPATH
strlcpy(myname, pw->pw_name, sizeof myname);
+ pthread_mutex_unlock(&pwdb_lock);
p = realpath(path, real);
if (p == NULL)
@@ -4767,6 +4766,7 @@ dkimf_securefile(const char *path, ino_t *ino, uid_t myuid, char *err,
return 1;
#else /* HAVE_REALPATH */
+ pthread_mutex_unlock(&pwdb_lock);
struct stat s;
status = stat(path, &s);