tensorflow
cifar10
error-handling
python
deep-learning

tensorflow cifar10_eval.py errorRuntimeError Attempted to use a closed Session.RuntimeError Attempted to use a closed Session

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The RuntimeError: Attempted to use a closed Session error in TensorFlow 1.x occurs when code tries to run operations (like session.run() or tensor.eval()) after the session has been closed or has exited its with block. In the CIFAR-10 evaluation script (cifar10_eval.py), this typically happens because the evaluation loop continues running after a tf.Session() context manager has exited, or because a checkpoint restoration triggers operations outside the active session scope. The fix involves restructuring the code to keep all operations within the session's lifetime.

Understanding TensorFlow Sessions

python
1import tensorflow as tf
2
3# TensorFlow 1.x session lifecycle
4graph = tf.Graph()
5with graph.as_default():
6    a = tf.constant(5)
7    b = tf.constant(3)
8    c = a + b
9
10# Method 1: Context manager (session auto-closes)
11with tf.Session(graph=graph) as sess:
12    result = sess.run(c)
13    print(result)  # 8
14# Session is CLOSED here
15
16# This will fail:
17# sess.run(c)  # RuntimeError: Attempted to use a closed Session
18
19# Method 2: Manual close
20sess = tf.Session(graph=graph)
21result = sess.run(c)
22sess.close()
23# sess.run(c)  # RuntimeError: Attempted to use a closed Session

The CIFAR-10 Evaluation Error

The typical problematic pattern in cifar10_eval.py:

python
1# PROBLEMATIC — session closes before eval loop finishes
2def evaluate():
3    with tf.Graph().as_default() as g:
4        images, labels = cifar10.inputs(eval_data='test')
5        logits = cifar10.inference(images)
6        top_k_op = tf.nn.in_top_k(logits, labels, 1)
7
8        saver = tf.train.Saver()
9
10        with tf.Session() as sess:
11            ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
12            if ckpt and ckpt.model_checkpoint_path:
13                saver.restore(sess, ckpt.model_checkpoint_path)
14            # Session closes at end of `with` block
15
16        # ERROR: trying to use the closed session
17        coord = tf.train.Coordinator()
18        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
19        # RuntimeError: Attempted to use a closed Session

Fix: Keep Everything Inside the Session Block

python
1def evaluate():
2    with tf.Graph().as_default() as g:
3        images, labels = cifar10.inputs(eval_data='test')
4        logits = cifar10.inference(images)
5        top_k_op = tf.nn.in_top_k(logits, labels, 1)
6
7        saver = tf.train.Saver()
8
9        with tf.Session() as sess:
10            ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
11            if ckpt and ckpt.model_checkpoint_path:
12                saver.restore(sess, ckpt.model_checkpoint_path)
13            else:
14                print('No checkpoint found')
15                return
16
17            # Start queue runners INSIDE the session block
18            coord = tf.train.Coordinator()
19            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
20
21            try:
22                num_iter = int(math.ceil(FLAGS.num_examples / FLAGS.batch_size))
23                true_count = 0
24                step = 0
25
26                while step < num_iter and not coord.should_stop():
27                    predictions = sess.run([top_k_op])
28                    true_count += np.sum(predictions)
29                    step += 1
30
31                precision = true_count / (step * FLAGS.batch_size)
32                print('precision @ 1 = %.3f' % precision)
33            except Exception as e:
34                coord.request_stop(e)
35            finally:
36                coord.request_stop()
37                coord.join(threads)

Fix for Continuous Evaluation Loop

python
1def eval_once(saver, top_k_op):
2    """Run evaluation once — session is created and closed each time."""
3    with tf.Session() as sess:
4        ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
5        if ckpt and ckpt.model_checkpoint_path:
6            saver.restore(sess, ckpt.model_checkpoint_path)
7            global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
8        else:
9            print('No checkpoint found')
10            return
11
12        coord = tf.train.Coordinator()
13        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
14
15        try:
16            num_iter = int(math.ceil(FLAGS.num_examples / FLAGS.batch_size))
17            true_count = 0
18            for step in range(num_iter):
19                predictions = sess.run([top_k_op])
20                true_count += np.sum(predictions)
21
22            precision = true_count / (num_iter * FLAGS.batch_size)
23            print('Step %s: precision @ 1 = %.3f' % (global_step, precision))
24        except Exception as e:
25            coord.request_stop(e)
26        finally:
27            coord.request_stop()
28            coord.join(threads)
29
30
31def evaluate():
32    """Evaluate periodically."""
33    with tf.Graph().as_default() as g:
34        images, labels = cifar10.inputs(eval_data='test')
35        logits = cifar10.inference(images)
36        top_k_op = tf.nn.in_top_k(logits, labels, 1)
37        saver = tf.train.Saver()
38
39        while True:
40            eval_once(saver, top_k_op)
41            time.sleep(FLAGS.eval_interval_secs)

Migration to TensorFlow 2.x

TensorFlow 2.x uses eager execution by default, eliminating sessions entirely.

python
1import tensorflow as tf
2
3# TF2 — no sessions needed
4model = tf.keras.models.load_model('saved_model/')
5
6# Evaluate directly
7(_, _), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
8x_test = x_test / 255.0
9
10loss, accuracy = model.evaluate(x_test, y_test)
11print(f'Test accuracy: {accuracy:.3f}')

Common Pitfalls

  • Operations outside the with tf.Session() block: Any sess.run(), tensor.eval(), or queue runner start after the with block exits will fail. Move all session-dependent code inside the with block, including coordinator and thread management.
  • Queue runners started outside the session scope: tf.train.start_queue_runners(sess=sess) must be called while the session is active. Starting queue runners after the session closes causes immediate failure.
  • Using sess.run() after sess.close(): If you manually manage sessions without a with block, calling sess.close() before all operations complete causes this error. Use the context manager pattern to ensure proper lifecycle management.
  • Checkpoint restoration in a different session: Creating a session, restoring a checkpoint, closing it, and then creating a new session loses the restored weights. Restore and evaluate in the same session.
  • Not migrating to TensorFlow 2.x: TF 1.x session management is error-prone. TF 2.x uses eager execution by default, eliminating the session concept entirely. If possible, migrate to TF 2.x where model.evaluate() handles everything.

Summary

  • RuntimeError: Attempted to use a closed Session means code is running operations after a session has been closed or exited its with block
  • Keep all TensorFlow operations (run, eval, queue runners) inside the with tf.Session() as sess: block
  • Start tf.train.Coordinator and queue runners inside the session scope
  • Use try/finally to ensure coord.request_stop() and coord.join(threads) are called
  • Migrate to TensorFlow 2.x to eliminate session management entirely

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design