Skip to content

run_task/blueapi hangs until it times out #1230

Closed

Description

@RJCD-Diamond

minimal example to show behaviour of run_task hanging.

log_detector on b21 can be run without interrupting normal operations

Looks like it's probably some race condition

import os
import time
from pathlib import Path
from blueapi.client.client import BlueapiClient
from blueapi.config import (
    ApplicationConfig,
    ConfigLoader,
)
from blueapi.service.model import TaskRequest
import saxs_bluesky.blueapi_configs

blueapi_config_path = "/dls_sw/b21/software/blueapi/scratch/saxs_bluesky/src/saxs_bluesky/blueapi_configs/b21_blueapi_config.yaml"

config_loader = ConfigLoader(ApplicationConfig)
config_loader.use_values_from_yaml(Path(blueapi_config_path))
loaded_config = config_loader.load()
CLIENT = BlueapiClient.from_config(loaded_config)

task = TaskRequest(
    name="log_detectors",
    params={},
    instrument_session="cm40643-4",
)

for i in range(10):
    print(i)
    CLIENT.run_task(task)
    time.sleep(5)

Activity

  1. added
    bug
    on Oct 10, 2025
  2. changed the title [-]run_task doesn't work, hangs until it times out, but also doesn't respect timeout[/-] [+]run_task/blueapi cli client doesn't work, hangs until it times out, but also doesn't respect timeout[/+] on Oct 10, 2025
  3. RJCD-Diamond commented on Oct 10, 2025

    @RJCD-Diamond
    ContributorAuthor

    Another minimal example with plans not specific to SCM beamline

    import os
    import time
    from pathlib import Path
    from blueapi.client.client import BlueapiClient
    from blueapi.config import (
        ApplicationConfig,
        ConfigLoader,
    )
    from blueapi.service.model import TaskRequest
    import saxs_bluesky.blueapi_configs
    from dodal.plan_stubs.wrapped import set_absolute
    
    blueapi_config_path = "ixx_blueapi_config.yaml"
    
    config_loader = ConfigLoader(ApplicationConfig)
    config_loader.use_values_from_yaml(Path(blueapi_config_path))
    loaded_config = config_loader.load()
    CLIENT = BlueapiClient.from_config(loaded_config)
    
    task = TaskRequest(
        name="set_absolute",
        params={"movable": SOMETHING, "value": 1},
        instrument_session="cm12345-1",
    )
    
    for i in range(10):
        print(i)
        CLIENT.run_task(task)
        time.sleep(5)

    It was hanging on the second iteration on b21 at the time, but is not consistent

  4. changed the title [-]run_task/blueapi cli client doesn't work, hangs until it times out, but also doesn't respect timeout[/-] [+]run_task/blueapi hangs until it times out, but also doesn't respect timeout[/+] on Oct 10, 2025
  5. RJCD-Diamond commented on Oct 14, 2025

    @RJCD-Diamond
    ContributorAuthor

    It's possible this will be fixed by #1232

  6. RJCD-Diamond commented on Oct 28, 2025

    @RJCD-Diamond
    ContributorAuthor

    #1232 has not fixed it

  7. changed the title [-]run_task/blueapi hangs until it times out, but also doesn't respect timeout[/-] [+]run_task/blueapi hangs until it times out[/+] on Nov 4, 2025
  8. abbiemery commented on Nov 4, 2025

    @abbiemery
    Contributor

    Can I ask what you are trying to do/why you are implementing this using time.sleep ? I don't know exactly what is happening without digging into it, but since this is a blocking call on that thread its a good candidate for shenanigans. If I know what you are trying to achieve we can do it in a way that works better with the eco-system.

  9. abbiemery commented on Nov 4, 2025

    @abbiemery
    Contributor

    It's hanging on the dodal wrapped versions of bps.set_absolute and set_relative at the moment.

    Putting your comment from slack here as notes.

  10. RJCD-Diamond commented on Nov 4, 2025

    @RJCD-Diamond
    ContributorAuthor

    Can I ask what you are trying to do/why you are implementing this using time.sleep ? I don't know exactly what is happening without digging into it, but since this is a blocking call on that thread its a good candidate for shenanigans. If I know what you are trying to achieve we can do it in a way that works better with the eco-system.

    You get the same behaviour with from dodal.plan_stubs.wrapped import sleep - if that's what you're wondering.

    The sleep is just there just to stop requests coming in too frequently, in case that was an issue.

    I'm trying to give a blueapi scripting interface for the beamline scientist for commissioning purposes. If I can give them the building blocks of the system, then they should have everything they need to start using it in ernest

  11. RJCD-Diamond commented on Nov 4, 2025

    @RJCD-Diamond
    ContributorAuthor

    Also when looking at the logs/messages of blueapi - the TaskRequest that it fails on is actually executed and is completed successfully without error

  12. RJCD-Diamond commented on Nov 4, 2025

    @RJCD-Diamond
    ContributorAuthor

    The absolutely most minimal example

    from pathlib import Path
    from blueapi.client.client import BlueapiClient
    from blueapi.config import (
        ApplicationConfig,
        ConfigLoader,
    )
    from blueapi.service.model import TaskRequest
    from dodal.plan_stubs.wrapped import set_absolute
    
    blueapi_config_path = "ixx_blueapi_config.yaml"
    
    config_loader = ConfigLoader(ApplicationConfig)
    config_loader.use_values_from_yaml(Path(blueapi_config_path))
    loaded_config = config_loader.load()
    CLIENT = BlueapiClient.from_config(loaded_config)
    
    task = TaskRequest(
        name="set_absolute",
        params={"movable": SOMETHING, "value": 1},
        instrument_session="cm12345-1",
    )
    
    for i in range(10):
        print(i)
        CLIENT.run_task(task)
  13. RJCD-Diamond commented on Nov 6, 2025

    @RJCD-Diamond
    ContributorAuthor

    Possibly related:

    #1160

  14. olliesilvester commented on Nov 21, 2025

    @olliesilvester
    Contributor

    @RJCD-Diamond Do you have the logs for when this last happened? We saw another weird thing happen on i04 today, I'm wondering if it's all related

  15. RJCD-Diamond commented on Nov 28, 2025

    @RJCD-Diamond
    ContributorAuthor

    No useful logs in graylog, however on the client side I get the following error:

    Silence this message by starting a CA repeater daemon
    or by calling ca_pend_event() and or ca_poll() more often.
    WARNING:root:Stomp connection lost, will attempt reconnection with policy StompReconnectPolicy(initial_delay=0.0, attempt_period=10.0)
    

    and in the BlueAPI logs I get (which looks normal) but never proceeds to the next iterations of the loop:

    2025-11-28 11:48:37,333 INFO Submitting: task_id='1c43f535-6825-4875-87e7-c8e3da33a226' task=Task(name='log_detectors', params={}, metadata={'instrument_session': 'cm40643-5'}) request_id=None is_complete=False is_pending=True errors=[]
    2025-11-28 11:48:37,334 INFO Got new task: task_id='1c43f535-6825-4875-87e7-c8e3da33a226' task=Task(name='log_detectors', params={}, metadata={'instrument_session': 'cm40643-5'}) request_id=None is_complete=False is_pending=True errors=[]
    2025-11-28 11:48:37,334 INFO Asked to run plan log_detectors with {} and metadata {'instrument_session': 'cm40643-5'} for all runs
    2025-11-28 11:48:37,334 INFO Executing plan <generator object log_detectors at 0x7fe9650971c0>
    2025-11-28 11:48:37,334 INFO Change state on <bluesky.run_engine.RunEngine object at 0x7fe965d96850> from 'idle' -> 'running'
    2025-11-28 11:48:37,335 INFO None
    2025-11-28 11:48:37,335 INFO Change state on <bluesky.run_engine.RunEngine object at 0x7fe965d96850> from 'running' -> 'idle'
    2025-11-28 11:48:37,335 INFO Cleaned up from plan <generator object log_detectors at 0x7fe9650971c0>
    2025-11-28 11:48:37,336 INFO Awaiting task
    2025-11-28 11:48:37,573 INFO method: POST url: http://i22-blueapi.diamond.ac.uk/tasks body: b'{"name": "log_detectors", "params": {}, "instrument_session": "cm40643-5"}'
    2025-11-28 11:48:37,575 WARNING Invalid correlation id detected: None
    
  16. RJCD-Diamond commented on Nov 28, 2025

    @RJCD-Diamond
    ContributorAuthor

    Also anecdotally it is a) worse during the day (died on about the 3 iter), compared to at night (5th iter) b) better during shutdown, c) better on a tuesday shutdown d) much better when the water leak happened (15th iter)

  17. olliesilvester commented on Nov 28, 2025

    @olliesilvester
    Contributor

    So yes, this is the same thing as we've seen a few times on i04. This is a high priority issue for MX too - until it's resolved it means we shouldn't really leave BlueAPI on a UDC beamline.

  18. tpoliaw commented on Dec 12, 2025

    @tpoliaw
    Contributor

    I've been digging into this and while I can't reproduce it from the example you have above, I did find a couple of related issues. I'm not sure if either is causing it on its own but it would be good to test with them fixed to check if they resolve this issue.

    The closest I could get to reproducing the problem was by stopping activemq while a scan was running - mainly inspired by the Stomp connection lost message you were getting in the client.

    • The stomp client doesn't reliably reconnect

      When the activemq service is restarted, the client never seems to reconnect to it meaning it never gets the 'task complete' message and waits indefinitely. This is potentially fixed by Catch and wrap unexpected connection errors bluesky-stomp#45 . With this patch included, the client can re-connect and continue listening for the end of the plan/task. There will still be an issue if the 'task complete' message is sent before the client can reconnect but this is a limitation of using a message bus.

      The default time to wait before attempting to reconnect is 10s. For the client, this might benefit from being reduced, or changed to have a slow backoff (1s, 2s, 3s, 5s, 10s,... etc).

    • The server aborts a task if it can't send messages

      When the server broadcasts events, if any of the callbacks fail, the exception is not sent to any other callbacks and the exception breaks the worker out of the task that it is running. This means if the server loses its connection the message bus, it aborts the task without sending the 'task complete' message meaning the client hangs indefinitely.

      This is potentially fixed in fix: Handle errors from event stream callbacks #1302. Combined with the bluesky-stomp fix, the server will continue with the task it is running and when the message bus is available again it will continue sending events. If this is before the task is complete, this will allow the client to continue. Again, if the task completes before the message bus is available we'll be back in the infinite wait scenario again.

  19. olliesilvester commented on Jan 28, 2026

    @olliesilvester
    Contributor

    Thanks for looking into this Peter! I'll post again here if I see the issue again

  20. added
    needs confirmation
    and removed on Feb 25, 2026
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

    bugneeds confirmation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions