# Thread unsafe hash usage in Logster

**URL:** https://meta.discourse.org/t/thread-unsafe-hash-usage-in-logster/225332
**Category:** Bug
**Created:** [April 26, 2022, 3:13pm UTC](https://meta.discourse.org/t/thread-unsafe-hash-usage-in-logster/225332 "2022-04-26T15:13:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![bjfish](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bjfish/32/188318_2.png) [@bjfish](https://meta.discourse.org/u/bjfish)
#### Post date: [April 26, 2022, 3:13pm UTC](https://meta.discourse.org/t/thread-unsafe-hash-usage-in-logster/225332/1 "2022-04-26T15:13:47Z")

</div>

While using discourse in a multi-threaded Ruby implementation and server (TruffleRuby/Puma), errors are produced by the unsafe hash usage in the logster gem near this section:

[https://github.com/discourse/logster/blob/31849a4154c6b1edb05fe6d241076329aa228359/lib/logster/logger.rb#L23-L29](https://github.com/discourse/logster/blob/31849a4154c6b1edb05fe6d241076329aa228359/lib/logster/logger.rb#L23-L29)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [April 26, 2022, 8:59pm UTC](https://meta.discourse.org/t/thread-unsafe-hash-usage-in-logster/225332/2 "2022-04-26T20:59:08Z")

</div>

I see, any chance you can add a Mutex there in a PR? seems like the correct approach to fixing this.

(aside [@dev-managers](https://meta.discourse.org/groups/dev-managers) / @jomaxro I guess for parity with other projects we should keep logster issues enabled on github? my vote is yes)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [April 26, 2022, 10:10pm UTC](https://meta.discourse.org/t/thread-unsafe-hash-usage-in-logster/225332/3 "2022-04-26T22:10:57Z")

</div>

A post was split to a new topic: [On which Discourse managed gems should we enable GitHub issues](https://meta.discourse.org/t/on-which-discourse-managed-gems-should-we-enable-github-issues/225369)

---

<div class="post-metadata">

### Author: ![eregon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eregon/32/188324_2.png) [@eregon](https://meta.discourse.org/u/eregon)
#### Post date: [April 27, 2022, 3:15pm UTC](https://meta.discourse.org/t/thread-unsafe-hash-usage-in-logster/225332/4 "2022-04-27T15:15:39Z")

</div>

If a Mutex is used, it should be used on both the write and read accesses for correctness.

This functionality feels like a better fit for fiber/thread-local variables though.  
Is it possible to have multiple Logster::Logger instances, or is it always just one? If multiple we somehow need to make the instance’s object\_id part of the key used for fiber/thread-local lookups.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [April 28, 2022, 1:14am UTC](https://meta.discourse.org/t/thread-unsafe-hash-usage-in-logster/225332/5 "2022-04-28T01:14:38Z")

</div>

Absolutely! I think this should fix it and heavily simplify this code at the same time :+1:

[https://github.com/discourse/logster/pull/160](https://github.com/discourse/logster/pull/160)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [April 28, 2022, 1:21am UTC](https://meta.discourse.org/t/thread-unsafe-hash-usage-in-logster/225332/6 "2022-04-28T01:21:33Z")

</div>

> [@eregon](#):
>
> Is it possible to have multiple Logster::Logger instances,

Pretty sure there is only one, but I added some protections.

I opted out of using a `define_finalizer` consumers are responsible for clean up, otherwise the accounting gets very hairy.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [May 1, 2022, 10:00pm UTC](https://meta.discourse.org/t/thread-unsafe-hash-usage-in-logster/225332/7 "2022-05-01T22:00:56Z")

</div>

This topic was automatically closed after 3 days. New replies are no longer allowed.
