diff --git a/session.h b/session.h
index 5a4569e39902e111240d3ea8ce6356294faf021a..e52b074739b3223740069f3b4777ee0428aada9c 100644
--- a/session.h
+++ b/session.h
@@ -213,6 +213,10 @@ struct serversession {
 	/* The numeric address they connected from, used for logging */
 	char * addrstring;
 
+#ifdef __uClinux__
+	pid_t server_pid;
+#endif
+
 };
 
 typedef enum {
diff --git a/svr-session.c b/svr-session.c
index eaccfe531fd81b7951460932d60d57dcdb07e42c..89b16cb2fc1c8ddeec28259fe22c413fb2e28761 100644
--- a/svr-session.c
+++ b/svr-session.c
@@ -85,6 +85,10 @@ void svr_session(int sock, int childpipe,
 	/* Initialise server specific parts of the session */
 	svr_ses.childpipe = childpipe;
 	svr_ses.addrstring = addrstring;
+#ifdef __uClinux__
+	svr_ses.server_pid = getpid();
+#endif
+	svr_ses.addrstring = addrstring;
 	svr_authinitialise();
 	chaninitialise(svr_chantypes);
 	svr_chansessinitialise();
@@ -144,11 +148,20 @@ void svr_dropbear_exit(int exitcode, const char* format, va_list param) {
 
 	_dropbear_log(LOG_INFO, fmtbuf, param);
 
-	/* free potential public key options */
-	svr_pubkey_options_cleanup();
+#ifdef __uClinux__
+	/* only the main server process should cleanup - we don't want
+	 * forked children doing that */
+	if (svr_ses.server_pid == getpid())
+#else
+	if (1)
+#endif
+	{
+		/* free potential public key options */
+		svr_pubkey_options_cleanup();
 
-	/* must be after we've done with username etc */
-	common_session_cleanup();
+		/* must be after we've done with username etc */
+		common_session_cleanup();
+	}
 
 	exit(exitcode);