diff --git a/lustre/ldlm/ldlm_lib.c b/lustre/ldlm/ldlm_lib.c
index e56ab1a45f..7f23e4e9fd 100644
--- a/lustre/ldlm/ldlm_lib.c
+++ b/lustre/ldlm/ldlm_lib.c
@@ -728,6 +728,17 @@ out_ldlm:
 		cli->cl_conn_count--;
 		class_disconnect(*exp);
 		*exp = NULL;
+		/*
+		 * Connect failed before obd_disconnect() could be called, so
+		 * client_disconnect_export() was never called and
+		 * ldlm_namespace_free_prior() was skipped.  Call it now so
+		 * that LDLM_NS_STOPPING is set before ldlm_namespace_free_post()
+		 * runs in client_obd_cleanup(), preventing a kobject leak and
+		 * the pool recalc race on the namespace.
+		 */
+		if (obd->obd_namespace != NULL)
+			ldlm_namespace_free_prior(obd->obd_namespace, imp,
+						  obd->obd_force);
 	}
 out_sem:
 	up_write(&cli->cl_sem);
diff --git a/lustre/osp/lwp_dev.c b/lustre/osp/lwp_dev.c
index 21a79e5e76..d47cc581e6 100644
--- a/lustre/osp/lwp_dev.c
+++ b/lustre/osp/lwp_dev.c
@@ -537,6 +537,17 @@ out_dis:
 		class_disconnect(*exp);
 		*exp = NULL;
 		lwp->lpd_exp = NULL;
+		/*
+		 * Connect failed before obd_disconnect() could be called, so
+		 * client_disconnect_export() was never called and
+		 * ldlm_namespace_free_prior() was skipped.  Call it now so
+		 * that LDLM_NS_STOPPING is set before ldlm_namespace_free_post()
+		 * runs in client_obd_cleanup(), preventing a kobject leak and
+		 * the pool recalc race on the namespace.
+		 */
+		if (obd->obd_namespace != NULL)
+			ldlm_namespace_free_prior(obd->obd_namespace, imp,
+						  obd->obd_force);
 	}
 
 out_sem:
