From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: alina sireneva <alina@tei.su>
Date: Mon, 20 Apr 2026 00:37:37 +0300
Subject: prevent the app from getting stuck when closing the attach panel before it fully opens

---
 .../ui/Components/ChatAttachAlert.java        | 41 ++++++++++---------
 1 file changed, 22 insertions(+), 19 deletions(-)

diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAttachAlert.java b/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAttachAlert.java
index 0000000..0000000 100644
--- a/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAttachAlert.java
+++ b/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatAttachAlert.java
@@ -5311,46 +5311,49 @@ public class ChatAttachAlert extends BottomSheet implements NotificationCenter.N
         currentSheetAnimation.setInterpolator(openInterpolator);
         AnimationNotificationsLocker locker = new AnimationNotificationsLocker();
         BottomSheetDelegateInterface delegate = super.delegate;
+        final boolean[] finished = { false };
+        final boolean[] wasCancelled = { false };
         final Runnable onAnimationEnd = () -> {
-            currentSheetAnimation = null;
-            appearSpringAnimation = null;
+            // idempotent: rapid dismiss cancels one animator mid-flight; original guards
+            // required both to end naturally, leaking the locker for ~5s. on cancel, leave
+            // currentSheetAnimation/appearSpringAnimation non-null so the outer
+            // cancelSheetAnimation() can still cancel the spring — otherwise it keeps
+            // pulling translation_Y to 0 and the dismiss animator can't visually close.
+            if (finished[0]) return;
+            finished[0] = true;
             locker.unlock();
-            currentSheetAnimationType = 0;
-            if (delegate != null) {
-                delegate.onOpenAnimationEnd();
-            }
             if (useHardwareLayer) {
                 container.setLayerType(View.LAYER_TYPE_NONE, null);
             }
-
             if (isFullscreen) {
                 WindowManager.LayoutParams params = getWindow().getAttributes();
                 params.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                 getWindow().setAttributes(params);
             }
             NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.startAllHeavyOperations, 512);
+            if (!wasCancelled[0]) {
+                currentSheetAnimation = null;
+                appearSpringAnimation = null;
+                if (delegate != null) {
+                    delegate.onOpenAnimationEnd();
+                }
+            }
         };
         appearSpringAnimation.addEndListener((animation, cancelled, value, velocity) -> {
-            if (currentSheetAnimation != null && !currentSheetAnimation.isRunning()) {
-                onAnimationEnd.run();
-            }
+            if (cancelled) wasCancelled[0] = true;
+            currentSheetAnimationType = 0;
+            onAnimationEnd.run();
         });
         currentSheetAnimation.addListener(new AnimatorListenerAdapter() {
             @Override
             public void onAnimationEnd(Animator animation) {
-                if (currentSheetAnimation != null && currentSheetAnimation.equals(animation)) {
-                    if (appearSpringAnimation != null && !appearSpringAnimation.isRunning()) {
-                        onAnimationEnd.run();
-                    }
-                }
+                onAnimationEnd.run();
             }
 
             @Override
             public void onAnimationCancel(Animator animation) {
-                if (currentSheetAnimation != null && currentSheetAnimation.equals(animation)) {
-                    currentSheetAnimation = null;
-                    currentSheetAnimationType = 0;
-                }
+                wasCancelled[0] = true;
+                onAnimationEnd.run();
             }
         });
         locker.lock();
