# streaming preview beachballs when URL is supplied

#### 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. Posted by Will Angley on 16 Feb, 2021 04:26 AM

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 Posted by Brett on 16 Feb, 2021 01:28 PM

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 Posted by Brett on 16 Feb, 2021 01:28 PM

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

-Brett

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

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 Posted by Brett on 17 Feb, 2021 12:27 PM

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. Posted by Will Angley on 18 Feb, 2021 08:38 PM

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. Posted by Will Angley on 20 Feb, 2021 01:35 AM

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 Posted by Brett on 20 Feb, 2021 01:45 AM

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. Posted by Will Angley on 20 Feb, 2021 06:08 PM

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 Posted by Brett on 21 Feb, 2021 01:17 PM

Let me know if you do find the cause.

- Brett

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

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 Posted by Brett on 23 Feb, 2021 12:59 PM

Awesome, will try this out!

- Brett

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

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 Posted by Brett on 23 Feb, 2021 01:21 PM

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. Posted by Will Angley on 23 Feb, 2021 06:39 PM

Brett,

• 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

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

### »

#### 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 Blurs the current field

### Comment Form

r Focus the comment reply box Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

## Recent Discussions

 26 Feb, 2021 02:24 PM *manual* refresh needed when using mathjax from script 24 Feb, 2021 06:25 AM pdflatex not found 23 Feb, 2021 06:39 PM streaming preview beachballs when URL is supplied 21 Feb, 2021 05:24 PM LanguageTool support 21 Feb, 2021 01:11 PM Obsidian and link support

## Recent Articles

 Intra-document links in exported PDFs Using Ulysses with Marked Fenced code blocks inside indented code blocks Using JavaScript in Marked Custom CSS: Writing custom CSS for Marked