Bokeh Memory Leak

Hi, I am running a bokeh server and started getting the following

Module <module 'bokeh_app_f6b4a122a1a348aca7596e9a77d04393' from '...app.py'> has extra unexpected referrers! This could indicate a serious memory leak. Extra referrers: [<cell at 0x0000025604200160: module object at 0x0000025604209720>]

I am unable to create a minimal reproducible example and my app is rather large with many moving parts built around Panel. I thought I would ask here first to see if there is a quick explanation to this. Running bokeh 2.4.2

Thanks

This is for certain dependent on some specific aspect of your (or potentially Panel’s) usage of Bokeh. I am am afraid we cannot really even begin to speculate without an MRE. All I can try to do is explain the message in more lay-terms in case that gives you some avenues to investigate yourself.

When Bokeh app sessions are created, a new module is created for the session (e.g. bokeh_app_f6b4a122a1a348aca7596e9a77d04393) and then the app code is executed inside that module. When the session is destroyed, Bokeh deletes the module and everything in it. As a precaution, Bokeh also checks that after it has deleted everything it knows about, that there is nothing left holding a reference to the module. That is where this message comes in. There should be nothing left, but in your case something (not Bokeh itself), somehow has held a reference to the session module, preventing it from being deleted. To be honest, that’s not easy to even do. You would have to be mucking around very low/private levels of Bokeh, or making custom Handler classes. [1] If that does not sound like anything you are doing, then the issue most likely originates in Panel, since Panel would be working at those levels. But Panel is a separate project maintained by a different team of people. Questions about Panel should be directed at the HoloViz Discourse:


  1. I guess it may not impossible that some obscure combination of app code itself could contrive some reference cycle that could trigger this, but I am not certain if that is true. I guess the only thing I can suggest to look for is if somehow you are saving references to Bokeh app objects in some “permanent” place e.g. literally assigning to attributes on outside modules like numpy.plot = myplot or something. ↩︎

Thanks for the explanation Bryan. Definitely not messing with the low levels of Bokeh so I will keep investigating more on the Panel side.

Much appreciated.

@ABarra555

This is recurring topic in panel with others facing precisely the “unexpected referrers” message. See, for example this topic in the panel discourse from June 2021.

Unfortunately, there is a lot of anecdotal feedback of different setup and features that cause the problem to manifest for individual users without any conclusive causes. Perhaps some of those experiences can help you formulate a workaround if not a fix based on proper root cause analysis.

Perhaps more relevant is the following GitHub issues for panel – currently still open – that is tracking this. See

For completeness, there is a related issue under bokeh’s GitHub, which is since closed, that has some good analysis and details of causes in certain scenarios.