streaming preview beachballs when URL is supplied

Will Angley's Avatar

Will Angley

16 Feb, 2021 03:47 AM

Hi Brett,

I'm writing a Sublime Text plugin that opens a buffer in Marked 2's Streaming Preview, and I think I've hit a bug. When I pass plain text, everything works fine. When I pass plain text and a base URL, Marked beachballs for ~15s, or sometimes longer.

I thought it was a sandboxing issue at first, since I was running the Mac App Store version when I started coding this earlier today. But I don't think it is – it happens consistently for me across both Catalina and Big Sur, and on the unsandboxed version.

Marked version: 2.6.4

Cheers,
Will

  1. 1 Posted by Will Angley on 16 Feb, 2021 04:26 AM

    Will Angley's Avatar

    I turned on debug logging in Marked, opened up Activity Monitor, and gathered debug information while reproducing this issue:

    • Generate preview at 22:57:00
    • Request spindump at 22:57:03
    • Marked un-freezes and writes logs at 22:57:57

    I've attached both the spindump and the logs that Marked wrote out.

  2. Support Staff 2 Posted by Brett on 16 Feb, 2021 01:28 PM

    Brett's Avatar

    How are you sending the base URL? It's expecting an NSURL in the URL
    pasteboard, not a plain text one. Based on the Marked logs it appears to
    be processing it fine, just wondering if it's getting gummed up in the
    conversion at all.

    It might help me debug if you could provide me with a working example I
    could run a trace on. I'm not a Python expert, so I need something I can
    just run from the command line, even if it has hardcoded text and URL.
    (I'm also a Sublime user, if sharing a package is easier.)

    -Brett

  3. Support Staff 3 Posted by Brett on 16 Feb, 2021 01:28 PM

    Brett's Avatar

    Also I'm excited that you're working on this, I would love a streaming
    package for Sublime!

    -Brett

  4. 4 Posted by Will Angley on 16 Feb, 2021 08:39 PM

    Will Angley's Avatar

    I'm sending the URL as an NSURL.

    Am working through my employer's Open Source Releasing process now to see if I can share the package. Will reply back when I hear back from them.

  5. Support Staff 5 Posted by Brett on 17 Feb, 2021 12:27 PM

    Brett's Avatar

    Here's the code I'm using for testing. I only know of one other developer that has for sure integrated the base url feature, so it's entirely possible there's an edge case I'm failing to compensate for. Seeing your code might help me find it. But this works (tested with the non-MAS version):

    #import <Foundation/Foundation.h>
    #import <Cocoa/Cocoa.h>
    #import <AppKit/AppKit.h>
    
    int main(int argc, char *argv[]) {
        @autoreleasepool {
            NSString *rawString = @"marked style: FadingFast\n\n![Connections button](images/connections-x.png)\n\n";
            NSURL *baseURL = [NSURL fileURLWithPath:@"/Users/ttscoff/Desktop/Code/nvultra-docs/content/"];
    //       pasteboard *must* be named 'mkStreamingPreview'
            NSPasteboard* pb = [NSPasteboard pasteboardWithName:@"mkStreamingPreview"];
            [pb clearContents];
            [pb writeObjects:@[rawString, baseURL]];
            [[NSWorkspace sharedWorkspace]
                openURL:[NSURL URLWithString:@"x-marked://stream?x-success=com.krill.CodeRunner-setapp"]];
        }
    }
    
  6. 6 Posted by Will Angley on 18 Feb, 2021 08:38 PM

    Will Angley's Avatar

    Thanks, Brett!

    Code is live now: https://github.com/willangley/PreviewInMarked

    My code looks like a near equivalent of it, but I'm not exactly sure. FWIW, I've been testing with Marked open already and haven't tried adding an openURL call.

  7. 7 Posted by Will Angley on 20 Feb, 2021 01:35 AM

    Will Angley's Avatar

    I'm impressed by the subtlety of this. I don't have CodeRunner, but I copied the code in #5 into a new Command Line Tool in Xcode and confirmed it opened Marked and then crashed Marked immediately.

    This wasn't what I was hoping for, but it wasn't a beachball, and then I realized I needed to revised the code a bit:

    1. pointed baseURL at a file on my system
    2. realized that openURL might rely on the destination app running at the time, and commented it out

    When I ran it again, it opened Marked and Marked became responsive immediately...

  8. Support Staff 8 Posted by Brett on 20 Feb, 2021 01:45 AM

    Brett's Avatar

    I clearly need a guard statement when reading the url…

    Is it possible the sublime plugin is sending an invalid url? I haven't had a chance to actually test it out yet.

    - Brett

  9. 9 Posted by Will Angley on 20 Feb, 2021 06:08 PM

    Will Angley's Avatar

    That's a good question – trying to answer it was revealing.

    I changed the plugin to write to [NSPasteboard generalPasteboard] and looked at the pasteboard contents in Pasteboard Viewer . When I did, I was able to access the text instantly; clicking on the file URL caused Pasteboard Viewer to hang for a while before displaying the URL.

    Pasteboard Viewer did not hang when I made a similar change to the code used for testing.

    Pasteboard Viewer doesn't just display file URLs; it will try to show a QuickLook preview of their contents. I'm using the Mac App Store version of this for now, so it won't actually show them even when I point to an extant path on the filesystem, but I'm guessing that trying to access the file at the URL is enough to result in a hang when it happens.

    I'm not sure the root cause of the hang yet, but it's not specific to Marked.

  10. Support Staff 10 Posted by Brett on 21 Feb, 2021 01:17 PM

    Brett's Avatar

    Let me know if you do find the cause.

    - Brett

  11. 11 Posted by Will Angley on 23 Feb, 2021 06:21 AM

    Will Angley's Avatar

    Brett,

    I did, and I was able to work around it. I've pushed an early-but-usable version to GitHub. LMK if you give it a try.


    A client that reads the URL from the pasteboard may ask for alternative representations, and pboard will try to call back to the process that wrote the URL.

    • If the process has exited, pboard returns immediately
    • If the process is running and has a main CFRunLoop, it'll answer pboard and pboard will return promptly
    • If the process is running and does not have a main CFRunLoop, pboard will wait for an answer and then timeout. The reader will hang while pboard is waiting.

    I don't control the Sublime Text plugin host, so I can't start a CFRunLoop on its main thread. But I can spawn a subprocess and start a CFRunLoop there...

    Cheers,
    Will

  12. Support Staff 12 Posted by Brett on 23 Feb, 2021 12:59 PM

    Brett's Avatar

    Awesome, will try this out!

    - Brett

  13. Support Staff 13 Posted by Brett on 23 Feb, 2021 01:15 PM

    Brett's Avatar

    By the way, in case it wasn't clear, the openURL in my example code just calls the Marked URL handler for opening the streaming preview and returning focus to the originating app by bundle identifier, in that case CodeRunner. I just left it there as an example of how the plugin could open the streaming preview. You wouldn't want to call it with every update, but it might be nice when first initializing. (You may have already implemented this, I haven 't tested yet!)

  14. Support Staff 14 Posted by Brett on 23 Feb, 2021 01:21 PM

    Brett's Avatar

    Ok, stupid question: I should be able to just drag the PreviewInMarked folder into Application Support/Sublime Text 3/Packages and it should show up in the palette right? It's been a long time since I manually installed a package...

  15. 15 Posted by Will Angley on 23 Feb, 2021 06:39 PM

    Will Angley's Avatar

    Brett,

    Thanks for trying this out! To answer your questions,

    • I have indeed integrated URL opening already! It's not perfect though; sometimes things load without styling or load to a blank page. Refreshing Marked should work around that. PreviewInMarked#4

    • That's not a stupid question, and that is indeed what you need to do. (And, if you've already got a PreviewInMarked folder locally, you should download it again – I fixed a pretty serious threading bug between then and now.)

      I'm hoping to get this stable enough to ship to Package Control over the weekend, since manual installs are a lot of work. PreviewInMarked#11

    Cheers,
    Will

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac