bokeh: [BUG] Cannot terminate busy pool processes in Bokeh app

Software versions

Python: 3.9.7 Bokeh version: 2.4.3 Tornado version: 6.1 Operating systems: Linux (Fedora 36, RHEL7, Ubuntu 18.04)

Expected behavior

In my application I have to put a timeout on a pool of processes executing jobs in parallel so that it is terminated if it takes too long. It works fine in a standalone script.

Observed behavior

In a Bokeh app the pool fails to terminate. The issue is serious because the main process hangs so that the app not only becomes unresponsive in the current session but cannot serve new sessions. The only solution it seems is to restart the server.

The minimal example provided works as expected when executed directly, but blocks at p.terminate() in the context of a Bokeh app (see output). The two processes that are still busy when p.terminate() is reached are not terminated (they fail to join in the _terminate_pool function of the pool.py module).

I do not think it’s a usage issue or a misuse of python’s multiprocessing but my apologies if it turns out that it is.

Note: originally filed an issue in Panel but it’s not related to Panel itself. I hope it’s not a consequence that is inherent to the implementation of Bokeh server.

Example code

import multiprocessing as mp
import time
import os
from bokeh.io import curdoc
from bokeh.models import Button


def task(num):
    print('Start', os.getpid(), num)
    time.sleep(num)
    print('End', os.getpid(), num)


def callback(new=None):

    p = mp.Pool(processes=4)
    res = p.map_async(task, [1, 2, 3, 4])
    try:
        res.get(timeout=2.5)
    except mp.context.TimeoutError:
        print('Timeout')
    p.terminate()
    print('Pool terminated')


button = Button()
button.on_click(callback)

curdoc().add_root(button)

if __name__ == '__main__':
    callback()

Stack traceback or browser console output

$ python script.py
Start 6209 1
Start 6210 2
Start 6211 3
Start 6212 4
End 6209 1
End 6210 2
Timeout
Pool terminated

$ bokeh serve script.py
2022-10-02 21:37:23,760 Starting Bokeh server version 2.4.3 (running on Tornado 6.1)
2022-10-02 21:37:23,761 User authentication hooks NOT provided (default user enabled)
2022-10-02 21:37:23,765 Bokeh app running at: http://localhost:5006/script
2022-10-02 21:37:23,766 Starting Bokeh server with process id: 7744
2022-10-02 21:37:28,903 404 GET /favicon.ico (127.0.0.1) 2.51ms
2022-10-02 21:37:28,904 WebSocket connection opened
2022-10-02 21:37:28,905 ServerConnection created
Start 7808 1
Start 7809 2
Start 7810 3
Start 7811 4
End 7808 1
End 7809 2
Timeout
Received signal 15, shutting down
Received signal 15, shutting down
End 7810 3
End 7811 4
# Hangs

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 15 (14 by maintainers)

Most upvoted comments

I don’t have any new information to add, unfortunately. I did just try putting the mp code into a more recent DocumentReady callback in case deferring the process spawns might help, but I got the same result as before.

def cb(event):
    p = mp.Pool(processes=4)
    res = p.map_async(task, [1, 2, 3, 4])
    try:
        res.get(timeout=2.5)
    except mp.context.TimeoutError:
        print('Timeout')
    p.terminate()
    print('Pool terminated')

curdoc().on_event("document_ready", cb)