diff --git a/src/ydb.c b/src/ydb.c
index 9759c3abd51..f569cfc7fef 100644
--- a/src/ydb.c
+++ b/src/ydb.c
@@ -740,6 +740,8 @@ toku_env_open(DB_ENV * env, const char *home, u_int32_t flags, int mode) {
     BOOL newenv;  // true iff creating a new environment
     u_int32_t unused_flags=flags;
 
+    most_recent_env = NULL;
+
     if (env_opened(env)) {
 	r = toku_ydb_do_error(env, EINVAL, "The environment is already open\n");
         goto cleanup;
@@ -2135,7 +2137,7 @@ toku_maybe_get_engine_status_text (char * buff, int buffsize) {
 static void 
 toku_maybe_set_env_panic(int code, char * msg) {
     DB_ENV * env = most_recent_env;
-    if (env) {
+    if (env && env->i) {
 	if (env->i->is_panicked == 0) {
 	    env->i->is_panicked = code;
 	    env->i->panic_string = toku_strdup(msg);