# Unexpected Termination of a Proc Callback in Crystal

**URL:** <https://forum.crystal-lang.org/t/unexpected-termination-of-a-proc-callback-in-crystal/7796>\
**Category:** Help & Support\
**Created:** [February 24, 2025, 2:59pm UTC](https://forum.crystal-lang.org/t/unexpected-termination-of-a-proc-callback-in-crystal/7796 "2025-02-24T14:59:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kojix2](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kojix2/32/1583_2.png) [@kojix2](https://forum.crystal-lang.org/u/kojix2)\
**Post date:** [February 24, 2025, 2:59pm UTC](https://forum.crystal-lang.org/t/unexpected-termination-of-a-proc-callback-in-crystal/7796/1 "2025-02-24T14:59:41Z")

</div>

A little while ago, I noticed that when passing a `Proc` as a callback function to a C function in Crystal, if it is not wrapped as a closure using the `Box` class, referencing an external variable causes Crystal to terminate in a way that feels mysterious to me at runtime. This phenomenon is neither a runtime error nor a segmentation fault? Rather, it seems that Crystal detects the anomaly and gracefully terminates the program.

What is the correct term to describe this behavior? Also, are there any other interesting edge cases in Crystal where compilation succeeds but unexpected termination occurs at runtime?

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [February 24, 2025, 3:47pm UTC](https://forum.crystal-lang.org/t/unexpected-termination-of-a-proc-callback-in-crystal/7796/2 "2025-02-24T15:47:47Z")

</div>

Could you show an example of the behaviour you describe?  
This really shouldn’t be happening.  
The compiler knows wether a proc references closured variables and should prevent passing them to lib functions.

```cr
fun c_call(proc : -> Int32) : Int32
  proc.call
end

x = 1
c_call ->() { x } # Error: can't send closure to C function (closured vars: x)

```

---

<div class="post-metadata">

**Author:** ![kojix2](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kojix2/32/1583_2.png) [@kojix2](https://forum.crystal-lang.org/u/kojix2)\
**Post date:** [February 24, 2025, 4:22pm UTC](https://forum.crystal-lang.org/t/unexpected-termination-of-a-proc-callback-in-crystal/7796/3 "2025-02-24T16:22:08Z")

</div>

I thought I saw that a few months ago, but remembered it today and asked the question… I probably won’t be able to reproduce it. Maybe I am remembering it wrong. But if I see this again I will report back. I thought this was an interesting behavior of Crystal, not a bug.
