[pigeon] Fix Kotlin generated sync host api error (#2693)

diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md
index d0beccf..ddf96c8 100644
--- a/packages/pigeon/CHANGELOG.md
+++ b/packages/pigeon/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 4.2.4
+
+* [kotlin] Fixes Kotlin generated sync host api error.
+
 ## 4.2.3
 
 * [java] Adds assert `args != null`.
diff --git a/packages/pigeon/lib/generator_tools.dart b/packages/pigeon/lib/generator_tools.dart
index 1f9a641..bc58a70 100644
--- a/packages/pigeon/lib/generator_tools.dart
+++ b/packages/pigeon/lib/generator_tools.dart
@@ -9,7 +9,7 @@
 import 'ast.dart';
 
 /// The current version of pigeon. This must match the version in pubspec.yaml.
-const String pigeonVersion = '4.2.3';
+const String pigeonVersion = '4.2.4';
 
 /// Read all the content from [stdin] to a String.
 String readStdin() {
diff --git a/packages/pigeon/lib/kotlin_generator.dart b/packages/pigeon/lib/kotlin_generator.dart
index d049f86..700c100 100644
--- a/packages/pigeon/lib/kotlin_generator.dart
+++ b/packages/pigeon/lib/kotlin_generator.dart
@@ -250,7 +250,7 @@
                   }
                 }, addTrailingNewline: false);
                 indent.add(' catch (exception: Error) ');
-                indent.scoped('{', '', () {
+                indent.scoped('{', '}', () {
                   indent.writeln(
                       'wrapped["${Keys.error}"] = wrapError(exception)');
                   if (method.isAsynchronous) {
@@ -262,7 +262,7 @@
                 }
               });
             }, addTrailingNewline: false);
-            indent.scoped('} else {', '}', () {
+            indent.scoped(' else {', '}', () {
               indent.writeln('channel.setMessageHandler(null)');
             });
           });
diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml
index c87de52..42674d6 100644
--- a/packages/pigeon/pubspec.yaml
+++ b/packages/pigeon/pubspec.yaml
@@ -2,7 +2,7 @@
 description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
 repository: https://github.com/flutter/packages/tree/main/packages/pigeon
 issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3Apigeon
-version: 4.2.3 # This must match the version in lib/generator_tools.dart
+version: 4.2.4 # This must match the version in lib/generator_tools.dart
 
 environment:
   sdk: ">=2.12.0 <3.0.0"
diff --git a/packages/pigeon/test/kotlin_generator_test.dart b/packages/pigeon/test/kotlin_generator_test.dart
index c9c8c6f..5bc0fed 100644
--- a/packages/pigeon/test/kotlin_generator_test.dart
+++ b/packages/pigeon/test/kotlin_generator_test.dart
@@ -126,6 +126,23 @@
     expect(code, contains('interface Api'));
     expect(code, contains('fun doSomething(input: Input): Output'));
     expect(code, contains('channel.setMessageHandler'));
+    expect(code, contains('''
+        if (api != null) {
+          channel.setMessageHandler { message, reply ->
+            val wrapped = hashMapOf<String, Any?>()
+            try {
+              val args = message as List<Any?>
+              val inputArg = args[0] as Input
+              wrapped["result"] = api.doSomething(inputArg)
+            } catch (exception: Error) {
+              wrapped["error"] = wrapError(exception)
+            }
+            reply.reply(wrapped)
+          }
+        } else {
+          channel.setMessageHandler(null)
+        }
+    '''));
   });
 
   test('all the simple datatypes header', () {