Skip to content

[Impeller][Vulkan] CommandPoolVK can free command buffers on a destroyed VkDevice, crashing in vkFreeCommandBuffers on the resource manager thread #186458

Closed

Description

@ZhongNing1

Steps to reproduce

1.Run a Flutter app on a Vulkan-enabled Android / Linux device with Impeller (e.g. --enable-impeller).
2.Repeatedly start and kill the app (or rapidly tear down the FlutterEngine / ContextVK).
3.Observe occasional native crashes during shutdown.
This is a teardown-time race; reproduction is timing-dependent but is reliably hit on lower-end devices under memory pressure or fast app-restart loops.

This is the same class of lifetime bug as #166725 ("[Impeller] keep device
holder and allocator alive until last vk image is destroyed"), but on the
command pool teardown path which was not touched in that PR.

Expected results

The application shuts down cleanly. vkDestroyCommandPool / vkFreeCommandBuffers should never be issued through a VkDevice that has already been destroyed.

Actual results

#00 vkFreeCommandBuffers+28 /system/lib64/libvulkan.so
#1 impeller::BackgroundCommandPoolVK::~BackgroundCommandPoolVK()
[inlined: impeller::CommandPoolRecyclerVK::Reclaim(
vk::UniqueHandle<vk::CommandPool, ...>&&,
std::vector<vk::UniqueHandle<vk::CommandBuffer, ...>>&&, bool)]
impeller/renderer/backend/vulkan/command_pool_vk.cc

Code sample

Code sample
[Paste your code here]

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[Paste your output here]

Activity

  1. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    engineflutter/engine related. See also e: labels.
    e: impellerImpeller rendering backend issues and features requests
    on May 13, 2026
  2. mbcorona commented on May 13, 2026

    @mbcorona
    Member

    Thanks for the detailed analysis and the reference to #166725.
    To help them act on this, could you provide:

    flutter doctor -v output
    Full crash log / stack trace (the snippet above appears truncated)
    Affected device(s) and OS version

  3. added
    c: crashStack traces logged to the console
    platform-androidAndroid applications specifically
    platform-linuxBuilding on or for Linux specifically
    on May 13, 2026
  4. ZhongNing1 commented on May 14, 2026

    @ZhongNing1
    Author
    Image

    Thanks for the detailed analysis and the reference to #166725. To help them act on this, could you provide:

    flutter doctor -v output Full crash log / stack trace (the snippet above appears truncated) Affected device(s) and OS version

  5. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 14, 2026
  6. mbcorona commented on May 14, 2026

    @mbcorona
    Member

    Hi @ZhongNing1
    The flutter doctor screenshot shows Flutter 2.35.7, which is well behind current stable (3.41.9). Could you first confirm whether this still reproduces on a recent stable or main build? Impeller's resource lifetime handling has had a number of changes since then, and it's possible this teardown path has already been addressed.
    If it does still reproduce, please also attach:

    Full flutter doctor -v output (text, not screenshot)
    The complete crash log / stack trace
    Affected device(s) and OS version

  7. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 14, 2026
  8. ZhongNing1 commented on May 15, 2026

    @ZhongNing1
    Author

    Hi @ZhongNing1 The flutter doctor screenshot shows Flutter 2.35.7, which is well behind current stable (3.41.9). Could you first confirm whether this still reproduces on a recent stable or main build? Impeller's resource lifetime handling has had a number of changes since then, and it's possible this teardown path has already been addressed. If it does still reproduce, please also attach:

    Full flutter doctor -v output (text, not screenshot) The complete crash log / stack trace Affected device(s) and OS version

    Sorry, due to certain reasons, we are unable to upgrade to version 3.41.9. However, from a code analysis perspective, this issue still persists in version 3.41.9.

  9. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 15, 2026
  10. mbcorona commented on May 15, 2026

    @mbcorona
    Member

    Given the specific signature (vkFreeCommandBuffers on destroyed VkDevice via CommandPoolRecyclerVK::Reclaim) and reference to the related #166725 fix which didn't cover this path, routing to the engine team for review of the lifetime guarantees rather than another repro request.

  11. 3 remaining items

  12. ZhongNing1 commented on May 19, 2026

    @ZhongNing1
    Author

    Can we get reproduction code please? Can this reproduce with just the standard Flutter counter app?

    The demo code is quite extensive and was generated during the stability test. It is necessary to make the following modifications.

    resolve_crash_diff.txt

  13. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 19, 2026
  14. jason-simmons commented on May 20, 2026

    @jason-simmons
    Contributor

    @ZhongNing1 Do you have a more complete stack trace for the situation shown in #186458 (comment)?

    The ContextVK owns the ResourceManagerVK. So in theory the ResourceManagerVK thread that deletes BackgroundCommandPoolVK instances should only be running while the ContextVK and its Vulkan device are alive.

    I'd like to get a better understanding of how a BackgroundCommandPoolVK was destructed after the Vulkan device was destroyed.

  15. ZhongNing1 commented on May 20, 2026

    @ZhongNing1
    Author

    @ZhongNing1 Do you have a more complete stack trace for the situation shown in #186458 (comment)?

    The ContextVK owns the ResourceManagerVK. So in theory the ResourceManagerVK thread that deletes BackgroundCommandPoolVK instances should only be running while the ContextVK and its Vulkan device are alive.

    I'd like to get a better understanding of how a BackgroundCommandPoolVK was destructed after the Vulkan device was destroyed.

    @jason-simmons

    ResourceManagerVK background thread starts
      └─ ResourceManagerVK::ResourceManagerVK()          [resource_manager_vk.cc:23]
           └─ waiter_ thread runs ResourceManagerVK::Start() [resource_manager_vk.cc:35]
                └─ thread name IplrVkResMgr              [resource_manager_vk.cc:41]
                     └─ while (!should_exit) { wait / swap reclaimables }  [resource_manager_vk.cc:47-58]
                          └─ resources_to_collect.clear() [resource_manager_vk.cc:69]   ← #02
                               └─ ~ResourceVKT<BackgroundCommandPoolVK>  (member resource_ at .h:129)
                                    └─ ~BackgroundCommandPoolVK()        [command_pool_vk.cc:40]  ← #01
                                         └─ CommandPoolRecyclerVK::Reclaim(...)  [command_pool_vk.cc:266]
                                              └─ device.resetCommandPool(...)    [command_pool_vk.cc:276-281]
                                              └─ destroy CommandBuffer / CommandPool (RAII)
                                                   └─ vkFreeCommandBuffers(...)  [libvulkan.so]  ← #00
    
  16. jason-simmons commented on May 20, 2026

    @jason-simmons
    Contributor

    The implementation of CommandPoolRecyclerVK::Reclaim in version 3.35.7 is:
    https://github.com/flutter/flutter/blob/3.35.7/engine/src/flutter/impeller/renderer/backend/vulkan/command_pool_vk.cc#L276

    The buffers.clear() line that calls vkFreeCommandBuffers happens after a call to context_.lock() that acquires a std::shared_ptr<ContextVK>. That indicates that the ContextVK associated with this CommandPoolRecyclerVK is alive, and it should contain a DeviceHolderVK with an active VkDevice.

    Can you confirm that the crash happens after the context_.lock() succeeds?

    If the ContextVK and its VkDevice are alive, then another possibility is that somehow one of the command buffers freed by CommandPoolRecyclerVK::Reclaim is bound to a different device.

    Try adding logging like this to CommandPoolRecyclerVK::Reclaim:

      FML_LOG(ERROR) << "*** context_device=" << static_cast<VkDevice>(device) << " pool.owner=" << pool.getOwner();
      if (should_trim) {
        for (const auto& buffer : buffers) {
          FML_LOG(ERROR) << "*** buffer.owner=" << buffer.getOwner();
        }
        buffers.clear();
        flags = vk::CommandPoolResetFlagBits::eReleaseResources;
      }
    

    Each vk::UniqueCommandPool and vk::UniqueCommandBuffer has an owner VkDevice that is used to delete the object. The VkDevice bound to these objects should be the same as the VkDevice held by the ContextVK.
    If there is a mismatch and the objects are owned by a different device which has been destroyed, then that could cause an error when the Vulkan objects are deleted.

    Also, is the application doing anything unusual such as creating multiple instances of the Flutter engine? Or using the FlutterEngineGroup engine cloning APIs?

  17. ZhongNing1 commented on May 21, 2026

    @ZhongNing1
    Author

    The issue does not involve the use of multiple engines or FlutterEngineGroup scenarios. The probability of this issue recurring in a similar scenario is low. Could the current issue be caused by insufficient memory?

  18. jason-simmons commented on May 21, 2026

    @jason-simmons
    Contributor

    It's unclear whether memory or other resource limitations could be a factor. Without a way to reproduce the issue, it's hard to determine why the vkFreeCommandBuffers call was failing.

  19. ZhongNing1 commented on May 22, 2026

    @ZhongNing1
    Author

    @jason-simmons

    Yes, we also hit this during stability testing, and I agree it is very hard to reproduce deterministically.

    That said, from a code perspective, this looks like the same class of lifetime issue as #166725: Vulkan objects are being destroyed on a background thread after the underlying VkDevice may already be gone.

  20. gaaclarke commented on May 22, 2026

    @gaaclarke
    Member

    @ZhongNing1, a fix has landed on the main channel based on your patch in #186458 (comment) ( #186749 )

    Try that out, please.

  21. github-actions commented on Aug 11, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  22. locked as resolved and limited conversation to collaborators on Aug 11, 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

    P2Important issues not at the top of the work listc: crashStack traces logged to the consolee: impellerImpeller rendering backend issues and features requestsengineflutter/engine related. See also e: labels.platform-androidAndroid applications specificallyplatform-linuxBuilding on or for Linux specificallyteam-engineOwned by Engine teamtriaged-engineTriaged by Engine team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions