<p dir="auto">As per <a href="https://groups.google.com/forum/#!topic/flutter-dev/mcbtlgRea-g" rel="nofollow">this thread</a>, platforms messages are relayed on and must be responded to on the main thread.</p>
<p dir="auto">This should probably be documented, both at the <a href="https://github.com/flutter/website/blob/master/platform-channels.md">platform-channels page</a> and in code documentation if relevant (FlutterResult, somewhere in FlutterBinaryMessenger, FlutterTextureRegister.textureFrameAvailable for a start).</p>
<p dir="auto">I started trying to write something to go into the platform-channels page at least but then realized that this is probably something that would warrant code examples at least in the page, if not an entire example project (unfortunately I don't have time right now to help with either of those).</p>
<p dir="auto">If it is something that might be considered for adding to the roadmap to be changed at some point, then maybe minimal documentation would be enough.</p>
<p dir="auto">This is what I started, if it's useful feel free to use it:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="## Threading in platform-specific code

Platform messages (including MethodChannel calls) are relayed and must be replied to
on the platform's main thread. Any tasks that run in background threads must relay
back to the main thread before responding.

This can be accomplished several different ways on each platform. A simple solution on
iOS is post tasks to [`Dispatch Queues`], while on android [`AsyncTask`] can 
be used to process data and then handle the response from the initial thread.

[AsyncTask]:https://developer.apple.com/documentation/dispatch
[Dispatch Queues]:https://developer.android.com/reference/android/os/AsyncTask.html"><pre class="notranslate"><code class="notranslate">## Threading in platform-specific code

Platform messages (including MethodChannel calls) are relayed and must be replied to
on the platform's main thread. Any tasks that run in background threads must relay
back to the main thread before responding.

This can be accomplished several different ways on each platform. A simple solution on
iOS is post tasks to [`Dispatch Queues`], while on android [`AsyncTask`] can 
be used to process data and then handle the response from the initial thread.

[AsyncTask]:https://developer.apple.com/documentation/dispatch
[Dispatch Queues]:https://developer.android.com/reference/android/os/AsyncTask.html
</code></pre></div>
<h2 dir="auto">Steps to Reproduce</h2>
<p dir="auto">Honestly, a bunch of things can cause Flutter to segfault.</p>
<ul dir="auto">
<li>
<p dir="auto">Scrolling a list with a bunch of images sometimes causes a segfault.</p>
</li>
<li>
<p dir="auto">Using the observatory sometimes causes a segfault.</p>
</li>
</ul>
<p dir="auto">Sometimes the segfaults don't even occur, which is even weirder.</p>
<h2 dir="auto">Backtrace</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="* thread #1, name = 'roid.synsthetic', stop reason = signal SIGSEGV: invalid address (fault address: 0x5)
  * frame #0: 0x96879d38 libflutter.so
    frame #1: 0x96878414 libflutter.so
    frame #2: 0x968782e8 libflutter.so
    frame #3: 0x96898736 libflutter.so
    frame #4: 0x9688026a libflutter.so
    frame #5: 0x96882dd8 libflutter.so
    frame #6: 0xb0eade88 libutils.so`android::Looper::pollInner(int) + 612
    frame #7: 0xb0eadb9a libutils.so`android::Looper::pollOnce(int, int*, int*, void**) + 30
    frame #8: 0xb16c8c9c libandroid_runtime.so`android::NativeMessageQueue::pollOnce(_JNIEnv*, _jobject*, int) + 26
    frame #9: 0x73859efe boot-framework.oat`nativePollOnce(this=&lt;unavailable&gt;, (null)=&lt;unavailable&gt;, (null)=&lt;unavailable&gt;) + 98
    frame #10: 0x9a31520d"><pre class="notranslate"><code class="notranslate">* thread #1, name = 'roid.synsthetic', stop reason = signal SIGSEGV: invalid address (fault address: 0x5)
  * frame #0: 0x96879d38 libflutter.so
    frame #1: 0x96878414 libflutter.so
    frame #2: 0x968782e8 libflutter.so
    frame #3: 0x96898736 libflutter.so
    frame #4: 0x9688026a libflutter.so
    frame #5: 0x96882dd8 libflutter.so
    frame #6: 0xb0eade88 libutils.so`android::Looper::pollInner(int) + 612
    frame #7: 0xb0eadb9a libutils.so`android::Looper::pollOnce(int, int*, int*, void**) + 30
    frame #8: 0xb16c8c9c libandroid_runtime.so`android::NativeMessageQueue::pollOnce(_JNIEnv*, _jobject*, int) + 26
    frame #9: 0x73859efe boot-framework.oat`nativePollOnce(this=&lt;unavailable&gt;, (null)=&lt;unavailable&gt;, (null)=&lt;unavailable&gt;) + 98
    frame #10: 0x9a31520d
</code></pre></div>
<h2 dir="auto">Memory map for libflutter.so</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Sections for 'C:\Users\ds841\.lldb\module_cache\remote-android\.cache\62357273-0000-0000-0000-000000000000\libflutter.so' (arm):
  SectID     Type             Load Address                             Perm File Off.  File Size  Flags      Section Name
  ---------- ---------------- ---------------------------------------  ---- ---------- ---------- ---------- ----------------------------
  0x00000001 regular                                                   ---  0x00000000 0x00000000 0x00000000 libflutter.so.
  0x00000002 elf-dynamic-symbols [0x00000000967ea114-0x00000000967edd64)  r--  0x00000114 0x00003c50 0x00000002 libflutter.so..dynsym
  0x00000003 regular          [0x00000000967edd64-0x00000000967f3d23)  r--  0x00003d64 0x00005fbf 0x00000002 libflutter.so..dynstr
  0x00000004 regular          [0x00000000967f3d24-0x00000000967f5464)  r--  0x00009d24 0x00001740 0x00000002 libflutter.so..hash
  0x00000005 regular          [0x00000000967f5464-0x00000000967f5bee)  r--  0x0000b464 0x0000078a 0x00000002 libflutter.so..gnu.version
  0x00000006 regular          [0x00000000967f5bf0-0x00000000967f5c0c)  r--  0x0000bbf0 0x0000001c 0x00000002 libflutter.so..gnu.version_d
  0x00000007 regular          [0x00000000967f5c0c-0x00000000967f5c6c)  r--  0x0000bc0c 0x00000060 0x00000002 libflutter.so..gnu.version_r
  0x00000008 elf-relocation-entries [0x00000000967f5c6c-0x000000009686fc84)  r--  0x0000bc6c 0x0007a018 0x00000002 libflutter.so..rel.dyn
  0x00000009 elf-relocation-entries [0x000000009686fc84-0x00000000968705cc)  r--  0x00085c84 0x00000948 0x00000042 libflutter.so..rel.plt
  0x0000000a regular          [0x00000000968705cc-0x00000000968713cc)  r-x  0x000865cc 0x00000e00 0x00000006 libflutter.so..plt
  0x0000000b code             [0x0000000096871400-0x0000000096eeda84)  r-x  0x00087400 0x0067c684 0x00000006 libflutter.so..text
  0x0000000c ARM.exidx        [0x0000000096eeda84-0x0000000096f1b17c)  r--  0x00703a84 0x0002d6f8 0x00000082 libflutter.so..ARM.exidx
  0x0000000d regular          [0x0000000096f1b180-0x00000000974b3cf6)  r--  0x00731180 0x00598b76 0x00000002 libflutter.so..rodata
  0x0000000e ARM.extab        [0x00000000974b3cf8-0x00000000974bcf08)  r--  0x00cc9cf8 0x00009210 0x00000002 libflutter.so..ARM.extab
  0x0000000f regular          [0x00000000974be400-0x0000000097507ff0)  rw-  0x00cd3400 0x00049bf0 0x00000003 libflutter.so..data.rel.ro
  0x00000010 regular          [0x0000000097507ff0-0x0000000097508108)  rw-  0x00d1cff0 0x00000118 0x00000003 libflutter.so..init_array
  0x00000011 regular          [0x0000000097508108-0x0000000097508110)  rw-  0x00d1d108 0x00000008 0x00000003 libflutter.so..fini_array
  0x00000012 elf-dynamic-link-info [0x0000000097508110-0x0000000097508240)  rw-  0x00d1d110 0x00000130 0x00000003 libflutter.so..dynamic
  0x00000013 regular          [0x0000000097508244-0x000000009750a000)  rw-  0x00d1d244 0x00001dbc 0x00000003 libflutter.so..got
  0x00000014 data             [0x000000009750a000-0x000000009750b298)  rw-  0x00d1f000 0x00001298 0x00000003 libflutter.so..data
  0x00000015 zero-fill        [0x000000009750b2a0-0x0000000097516dcc)  rw-  0x00d20298 0x00000000 0x00000003 libflutter.so..bss
  0x00000016 regular                                                   ---  0x00d20298 0x00000188 0x00000030 libflutter.so..comment
  0x00000017 regular                                                   ---  0x00d20420 0x0000001c 0x00000000 libflutter.so..note.gnu.gold-version
  0x00000018 regular                                                   ---  0x00d2043c 0x00000038 0x00000000 libflutter.so..ARM.attributes
  0x00000019 regular                                                   ---  0x00d20474 0x000000f0 0x00000000 libflutter.so..shstrtab"><pre class="notranslate"><code class="notranslate">Sections for 'C:\Users\ds841\.lldb\module_cache\remote-android\.cache\62357273-0000-0000-0000-000000000000\libflutter.so' (arm):
  SectID     Type             Load Address                             Perm File Off.  File Size  Flags      Section Name
  ---------- ---------------- ---------------------------------------  ---- ---------- ---------- ---------- ----------------------------
  0x00000001 regular                                                   ---  0x00000000 0x00000000 0x00000000 libflutter.so.
  0x00000002 elf-dynamic-symbols [0x00000000967ea114-0x00000000967edd64)  r--  0x00000114 0x00003c50 0x00000002 libflutter.so..dynsym
  0x00000003 regular          [0x00000000967edd64-0x00000000967f3d23)  r--  0x00003d64 0x00005fbf 0x00000002 libflutter.so..dynstr
  0x00000004 regular          [0x00000000967f3d24-0x00000000967f5464)  r--  0x00009d24 0x00001740 0x00000002 libflutter.so..hash
  0x00000005 regular          [0x00000000967f5464-0x00000000967f5bee)  r--  0x0000b464 0x0000078a 0x00000002 libflutter.so..gnu.version
  0x00000006 regular          [0x00000000967f5bf0-0x00000000967f5c0c)  r--  0x0000bbf0 0x0000001c 0x00000002 libflutter.so..gnu.version_d
  0x00000007 regular          [0x00000000967f5c0c-0x00000000967f5c6c)  r--  0x0000bc0c 0x00000060 0x00000002 libflutter.so..gnu.version_r
  0x00000008 elf-relocation-entries [0x00000000967f5c6c-0x000000009686fc84)  r--  0x0000bc6c 0x0007a018 0x00000002 libflutter.so..rel.dyn
  0x00000009 elf-relocation-entries [0x000000009686fc84-0x00000000968705cc)  r--  0x00085c84 0x00000948 0x00000042 libflutter.so..rel.plt
  0x0000000a regular          [0x00000000968705cc-0x00000000968713cc)  r-x  0x000865cc 0x00000e00 0x00000006 libflutter.so..plt
  0x0000000b code             [0x0000000096871400-0x0000000096eeda84)  r-x  0x00087400 0x0067c684 0x00000006 libflutter.so..text
  0x0000000c ARM.exidx        [0x0000000096eeda84-0x0000000096f1b17c)  r--  0x00703a84 0x0002d6f8 0x00000082 libflutter.so..ARM.exidx
  0x0000000d regular          [0x0000000096f1b180-0x00000000974b3cf6)  r--  0x00731180 0x00598b76 0x00000002 libflutter.so..rodata
  0x0000000e ARM.extab        [0x00000000974b3cf8-0x00000000974bcf08)  r--  0x00cc9cf8 0x00009210 0x00000002 libflutter.so..ARM.extab
  0x0000000f regular          [0x00000000974be400-0x0000000097507ff0)  rw-  0x00cd3400 0x00049bf0 0x00000003 libflutter.so..data.rel.ro
  0x00000010 regular          [0x0000000097507ff0-0x0000000097508108)  rw-  0x00d1cff0 0x00000118 0x00000003 libflutter.so..init_array
  0x00000011 regular          [0x0000000097508108-0x0000000097508110)  rw-  0x00d1d108 0x00000008 0x00000003 libflutter.so..fini_array
  0x00000012 elf-dynamic-link-info [0x0000000097508110-0x0000000097508240)  rw-  0x00d1d110 0x00000130 0x00000003 libflutter.so..dynamic
  0x00000013 regular          [0x0000000097508244-0x000000009750a000)  rw-  0x00d1d244 0x00001dbc 0x00000003 libflutter.so..got
  0x00000014 data             [0x000000009750a000-0x000000009750b298)  rw-  0x00d1f000 0x00001298 0x00000003 libflutter.so..data
  0x00000015 zero-fill        [0x000000009750b2a0-0x0000000097516dcc)  rw-  0x00d20298 0x00000000 0x00000003 libflutter.so..bss
  0x00000016 regular                                                   ---  0x00d20298 0x00000188 0x00000030 libflutter.so..comment
  0x00000017 regular                                                   ---  0x00d20420 0x0000001c 0x00000000 libflutter.so..note.gnu.gold-version
  0x00000018 regular                                                   ---  0x00d2043c 0x00000038 0x00000000 libflutter.so..ARM.attributes
  0x00000019 regular                                                   ---  0x00d20474 0x000000f0 0x00000000 libflutter.so..shstrtab
</code></pre></div>
<h2 dir="auto">Flutter Doctor</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[√] Flutter (on Microsoft Windows [Version 10.0.14393], locale en-US, channel master)
    • Flutter at C:\Users\ds841\Documents\flutter
    • Framework revision 9fb6fd81b9 (30 hours ago), 2017-09-15 07:37:08 -0700
    • Engine revision 0e8ffa24e0
    • Tools Dart version 1.25.0-dev.11.0

[√] Android toolchain - develop for Android devices (Android SDK 26.0.1)
    • Android SDK at C:\Users\ds841\AppData\Local\Android\sdk
    • Platform android-26, build-tools 26.0.1
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_112-release-b06)

[√] Android Studio (version 2.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Java version OpenJDK Runtime Environment (build 1.8.0_112-release-b06)

[√] IntelliJ IDEA Community Edition (version 2017.2)
    • Flutter plugin version 17.0
    • Dart plugin version 172.3968.27

[√] Connected devices
    • XT1635 01 • ZY223J6829 • android-arm • Android 7.1.1 (API 25)"><pre class="notranslate"><code class="notranslate">[√] Flutter (on Microsoft Windows [Version 10.0.14393], locale en-US, channel master)
    • Flutter at C:\Users\ds841\Documents\flutter
    • Framework revision 9fb6fd81b9 (30 hours ago), 2017-09-15 07:37:08 -0700
    • Engine revision 0e8ffa24e0
    • Tools Dart version 1.25.0-dev.11.0

[√] Android toolchain - develop for Android devices (Android SDK 26.0.1)
    • Android SDK at C:\Users\ds841\AppData\Local\Android\sdk
    • Platform android-26, build-tools 26.0.1
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_112-release-b06)

[√] Android Studio (version 2.3)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Java version OpenJDK Runtime Environment (build 1.8.0_112-release-b06)

[√] IntelliJ IDEA Community Edition (version 2017.2)
    • Flutter plugin version 17.0
    • Dart plugin version 172.3968.27

[√] Connected devices
    • XT1635 01 • ZY223J6829 • android-arm • Android 7.1.1 (API 25)
</code></pre></div>