amauryt / cr-cmark-gfm

Crystal C bindings for cmark-gfm to work with Commonmark and Github Flavored Markdown
BSD 2-Clause "Simplified" License
20 stars 4 forks source link

Get Invalid memory access #3

Closed confact closed 4 years ago

confact commented 4 years ago

I am using this in lucky to generate content for pages from markdown files.

I get this error after reloading the markdown page 2-4 times:

Invalid memory access (signal 11) at address 0x7f28f4a3c270
[0x5640c3437246] *CallStack::print_backtrace:Int32 +118
[0x5640c33f5410] __crystal_sigfault_handler +304
[0x7f28f9abb800] ???
[0x5640c3883915] ???
[0x5640c37ca99c] *Cmark::Node#finalize:Nil +188
[0x5640c34216fa] ~proc15Proc(Pointer(Void), Pointer(Void), Nil) +10
[0x5640c38b2c02] GC_invoke_finalizers +162
[0x5640c38b2d58] GC_notify_or_invoke_finalizers +168
[0x5640c38b4ce7] GC_generic_malloc_many +151
[0x5640c38c23ce] GC_malloc_kind +190
[0x5640c349faf6] *GC::malloc<UInt64>:Pointer(Void) +6
[0x5640c33df22e] __crystal_malloc64 +14

Is it something I have to think about when using this?

amauryt commented 4 years ago

This seems to be a problem with freeing the pointers of nodes in the underlying C library while Crystal garbage-collects an instance of Cmark::Node.

Can you provide more details (which version of Crystal are you using, environment, etc)?

Can you also provide a minimal example which reproduces the issue?

confact commented 4 years ago

Hi @amauryt, I have created a public repo that gives this error: https://github.com/confact/cmark-bug-test

There you can see I am using 0.34 of crystal. My operative system is Arch Linux.

You run the Lucky app like this:

confact commented 4 years ago

The class that is calling cmark is ./src/pages/faq/data_privacy_page.cr

amauryt commented 4 years ago

Hey @confact. I cloned the app and followed your instructions (you missed a "yarn install" :wink:) but I am unable to reproduce the issue. I'm constantly reloading on the browser the page you indicated and the Markdown is rendered without a problem. I am using MacOS though.

Screenshot 2020-05-22 at 20 15 56

Can you try your app on a different OS? Or perhaps you have different settings or do something different?

snadon commented 4 years ago

I am also this library and my project once deployed on Heroku crashes with Invalid memory access (signal 11) at address 0x7fbec3d59270 whenever I access a page that uses this library.

It works great on my Macbook Pro, so this is probably why @amauryt is not getting the error.

amauryt commented 4 years ago

Thank you for confirming the issue @snadon. It seems that I need to fix node garbage collection for Linux, but first I need more information.

snadon commented 4 years ago

Thank you for confirming the issue @snadon. It seems that I need to fix node garbage collection for Linux, but first I need more information.

* Are you also using Lucky to render your webpage with MD content, or another framework?

Yes. Here's the code for that page:

require "cmark"

class Home::IndexEnSlug < BrowserAction
  include Auth::AllowGuests

  get "/en/:slug" do
    lang = AppLang::EN.value
    doc = DocQuery.new
      .lang(lang)
      .slug(slug)
      .first?
    unless doc.nil?
      options = Cmark::Option.flags(Nobreaks, ValidateUTF8)
      extensions = Cmark::Extension.flags(Table)
      doc.content = Cmark.document_to_html(doc.content, options, extensions)
      html ShowPage, lang: lang, doc: doc
    else
      html Errors::ShowPage, message: "Not Found!", status: 404
    end
  end
end
class Docs::ShowPage < MainLayout
  needs doc : Doc
  quick_def page_title, "Page ##{doc.id}"

  def content
    link "← Back", Docs::Index
    h1 "Page (#{AppLang.new(doc.lang)}) ##{doc.id}"
    render_actions
    render_doc_fields
  end

  def render_actions
    section do
      link "Edit", Docs::Edit.with(doc.id), class: "button mr-2"
      link "Delete",
        Docs::Delete.with(doc.id),
        data_confirm: "Are you sure?",
        class: "button button-danger"
    end
  end

  def render_doc_fields
    div do
      raw doc.content
    end
  end
end
* Just to confirm, the page crashes all the time, right? Or only after a few reloads as in the first report?

Lately it was all the time, even after purging (heroku repo:purge_cache -a appname). However, IIRC, it was intermittent at the beginning of the project.

* Is your stack on Heroku v18 (Ubuntu 18), v16 or a container?

heroku-18

amauryt commented 4 years ago

Thanks for the info. This should be already fixed on master, please do a shards update. Otherwise feel free to reopen this issue.