Skip to content

Session got stuck after fork #2448

Closed
Closed

Description

@ppwwyyxx

Version: nightly prebuilt for Python2 w/ GPU (just now)

I'm expecting the following code to print "10.0" 3 times, but session.run got stuck in all forked processes.

import tensorflow as tf
import multiprocessing as mp
import os

class Worker(mp.Process):
    def __init__(self, gid):
        self.gid = gid
        super(Worker, self).__init__()

    def run(self):
        G = tf.Graph()
        with G.as_default():
            x = tf.placeholder(tf.float32, shape=[])
            y = x * 2
            sess = tf.Session()
            print sess.run(y, feed_dict={x: 5})

G = tf.Graph()
with G.as_default():
    sess = tf.Session()
    with sess.as_default():
        x = tf.placeholder(tf.float32, shape=[])
        y = x * 2
        print sess.run(y, feed_dict={x: 5})

procs = [Worker(k) for k in range(2)]
for p in procs: p.start()
for p in procs: p.join()

Removing the graph/session in master process will solve the problem. So it seems like once there is a session, we cannot use fork?
The problem exists with and without GPU.

NOTE: this code doesn't terminate normally. You'll probably need to manually kill the forked processes after the master exited.

Activity

  1. changed the title [-]Wierd Behavior after Fork[/-] [+]Weird Behavior after Fork[/+] on May 21, 2016
  2. changed the title [-]Weird Behavior after Fork[/-] [+]Session got stuck after fork[/+] on May 21, 2016
  3. mrry commented on May 21, 2016

    @mrry
    Contributor

    The in-process session (i.e. tf.Session() with no arguments) is not designed to be fork()-safe. If you want to share a set of devices between multiple processes, create a tf.train.Server in one process, and create sessions that connect to that server (with tf.Session("grpc://...")) in the other processes.

  4. mavenlin commented on Oct 20, 2016

    @mavenlin
    Contributor

    @mrry Does it mean there is a way to create fork safe tf.Session with tf.Session(args)?

  5. Liam0205 commented on Oct 20, 2016

    @Liam0205

    @mavenlin
    The prototype of tf.Session is

    tf.Session.__init__(target='', graph=None, config=None)

    Here target refers to the execution engine to connect to. That is, one has to run the working session in another process, in distributed mode, and tf.Session with arguments is still not fork()-safe.

    Sad news.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions