Repository: flutter
Issue: Opaque GestureDetector occasionally leaks mouse clicks through
Labels: c: crash,framework,f: gestures,has reproducible steps,P2,found in release: 3.3,found in release: 3.7,team-framework,triaged-framework

1. Execute `flutter run` on the code sample <!-- (see "Code sample" section below) -->
2. Repeatedly click anywhere in the red area _with a mouse_
3. Inspect the app output while repeatedly clicking

**Expected results:** <!-- what did you want to see? -->

The app prints `onTapDown` or `onSecondaryTapDown` without throwing assertion failures.

**Actual results:** <!-- what did you see? -->

Most of the time it works i.e. the app prints the event location correctly but occasionally, clicks leak through:

```
════════ Exception caught by gesture ═══════════════════════════════════════════
The following assertion was thrown while handling a gesture:
onTapDown leaked
'package:gesture_recognizer/main.dart':
package:gesture_recognizer/main.dart:1
Failed assertion: line 10 pos 22: 'false'


When the exception was thrown, this was the stack
#2      main.<anonymous closure>.<anonymous closure> package:gesture_recognizer/main.dart:10
#3      TapGestureRecognizer.handleTapDown.<anonymous closure> package:flutter/…/gestures/tap.dart:596
#4      GestureRecognizer.invokeCallback package:flutter/…/gestures/recognizer.dart:253
#5      TapGestureRecognizer.handleTapDown package:flutter/…/gestures/tap.dart:596
#6      BaseTapGestureRecognizer._checkDown package:flutter/…/gestures/tap.dart:297
#7      BaseTapGestureRecognizer.didExceedDeadline package:flutter/…/gestures/tap.dart:267
#8      PrimaryPointerGestureRecognizer.didExceedDeadlineWithEvent package:flutter/…/gestures/recognizer.dart:643
#9      PrimaryPointerGestureRecognizer.addAllowedPointer.<anonymous closure> package:flutter/…/gestures/recognizer.dart:586
(elided 6 frames from class _AssertionError, class _RawReceivePortImpl, class _Timer, and dart:async-patch)
Handler: "onTapDown"
Recognizer: TapGestureRecognizer#16d50
    debugOwner: GestureDetector
    state: possible
    button: 1
════════════════════════════════════════════════════════════════════════════════
```

```
════════ Exception caught by gesture ═══════════════════════════════════════════
onSecondaryTapDown leaked
'package:gesture_recognizer/main.dart':
package:gesture_recognizer/main.dart:1
Failed assertion: line 13 pos 22: 'false'

════════════════════════════════════════════════════════════════════════════════
```

<details>
<summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        body: Builder(builder: (context) {
          return GestureDetector(
            onTapDown: (d) {
              assert(false, 'onTapDown leaked');
            },
            onSecondaryTapDown: (d) {
              assert(false, 'onSecondaryTapDown leaked');
            },
            child: GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTapDown: (d) => print('onTapDown: ${d.localPosition}'),
              onSecondaryTapDown: (d) =>
                  print('onSecondaryTapDown: ${d.localPosition}'),
              child: Container(
                color: Colors.red,
                width: double.infinity,
                height: double.infinity,
              ),
            ),
          );
        }),
      ),
    ),
  );
}
```

</details>

<details>
  <summary>Logs</summary>

```
Launching lib/main.dart on Linux in debug mode...
Connecting to VM Service at ws://127.0.0.1:41967/jrUgYJoQE7E=/ws
flutter: onTapDown: Offset(587.0, 346.0)
flutter: onTapDown: Offset(584.0, 346.0)
flutter: onTapDown: Offset(584.0, 346.0)
flutter: onTapDown: Offset(584.0, 346.0)
flutter: onTapDown: Offset(584.0, 347.0)
flutter: onTapDown: Offset(586.0, 347.0)
flutter: onTapDown: Offset(585.0, 346.0)
flutter: onTapDown: Offset(584.0, 347.0)
flutter: onTapDown: Offset(583.0, 357.0)
flutter: onTapDown: Offset(584.0, 358.0)
flutter: onTapDown: Offset(584.0, 359.0)
flutter: onTapDown: Offset(581.0, 357.0)
flutter: onTapDown: Offset(580.0, 357.0)
flutter: onTapDown: Offset(580.0, 357.0)
flutter: onTapDown: Offset(579.0, 356.0)
flutter: onTapDown: Offset(579.0, 356.0)
flutter: onTapDown: Offset(579.0, 356.0)
flutter: onTapDown: Offset(589.0, 358.0)

════════ Exception caught by gesture ═══════════════════════════════════════════
The following assertion was thrown while handling a gesture:
onTapDown leaked
'package:gesture_recognizer/main.dart':
Failed assertion: line 10 pos 22: 'false'


When the exception was thrown, this was the stack
#2      main.<anonymous closure>.<anonymous closure>
#3      TapGestureRecognizer.handleTapDown.<anonymous closure>
#4      GestureRecognizer.invokeCallback
#5      TapGestureRecognizer.handleTapDown
#6      BaseTapGestureRecognizer._checkDown
#7      BaseTapGestureRecognizer.didExceedDeadline
#8      PrimaryPointerGestureRecognizer.didExceedDeadlineWithEvent
#9      PrimaryPointerGestureRecognizer.addAllowedPointer.<anonymous closure>
(elided 6 frames from class _AssertionError, class _RawReceivePortImpl, class _Timer, and dart:async-patch)
Handler: "onTapDown"
Recognizer: TapGestureRecognizer#721a2
    debugOwner: GestureDetector
    state: possible
    button: 1
════════════════════════════════════════════════════════════════════════════════
```

```
[✓] Flutter (Channel stable, 3.3.9, on Ubuntu 22.10 5.19.0-23-generic, locale en_DK.UTF-8)
    • Flutter version 3.3.9 on channel stable at /home/jpnurmi/.fvm/versions/stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b8f7f1f986 (2 weeks ago), 2022-11-23 06:43:51 +0900
    • Engine revision 8f2221fbef
    • Dart version 2.18.5
    • DevTools version 2.15.0

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • Ubuntu clang version 15.0.2-1
    • cmake version 3.24.2
    • ninja version 1.11.0
    • pkg-config version 0.29.2

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[✓] VS Code
    • VS Code at /snap/code/current
    • Flutter extension version 3.54.0

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Ubuntu 22.10 5.19.0-23-generic
    • Chrome (web)    • chrome • web-javascript • Google Chrome 108.0.5359.94

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.
```

```
code [✓] Flutter (Channel main, 3.7.0-3.0.pre.57, on Ubuntu 22.10 5.19.0-23-generic, locale en_DK.UTF-8)
    • Flutter version 3.7.0-3.0.pre.57 on channel main at /home/jpnurmi/Flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ef6ead4401 (8 hours ago), 2022-12-06 23:15:24 -0500
    • Engine revision 67254d6e4b
    • Dart version 2.19.0 (build 2.19.0-467.0.dev)
    • DevTools version 2.20.0

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • Ubuntu clang version 15.0.2-1
    • cmake version 3.24.2
    • ninja version 1.11.0
    • pkg-config version 0.29.2

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[✓] VS Code
    • VS Code at /snap/code/current
    • Flutter extension version 3.54.0

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Ubuntu 22.10 5.19.0-23-generic
    • Chrome (web)    • chrome • web-javascript • Google Chrome 108.0.5359.94

⣷[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.
```

</details>