# InitPaths is not threadsafe

**URL:** <https://discuss.elastic.co/t/initpaths-is-not-threadsafe/177519>\
**Category:** Beats\
**Created:** [April 18, 2019, 5:37pm UTC](https://discuss.elastic.co/t/initpaths-is-not-threadsafe/177519 "2019-04-18T17:37:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asanderson15](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asanderson15/32/45053_2.png) [@asanderson15](https://discuss.elastic.co/u/asanderson15)\
**Post date:** [April 18, 2019, 5:37pm UTC](https://discuss.elastic.co/t/initpaths-is-not-threadsafe/177519/1 "2019-04-18T17:37:59Z")

</div>

There is a race when using [InitPaths](https://github.com/elastic/beats/blob/master/libbeat/paths/paths.go#L94) during initial beat configuration if you initialize more than a single beat in parallel.

Calling `InitPaths` calls `Path.InitPaths` ([code](https://github.com/elastic/beats/blob/master/libbeat/paths/paths.go#L95)), where `Path` is a [shared global pointer](https://github.com/elastic/beats/blob/master/libbeat/paths/paths.go#L66). That method in turn calls `initPaths`, which [sets the global ptr to the Path ptr passed by the caller](https://github.com/elastic/beats/blob/master/libbeat/paths/paths.go#L101). This creates a race where, if `InitPaths` is called in parallel, the global `Path` ptr can be redirected before `MkdirAll` gets called, meaning that some paths are not properly created and that beat initialization will fail.

An easy fix to make this threadsafe would be to declare a global mutex corresponding to the `Paths` var and then to wrap the `Paths.InitPaths` call with it:

```auto
var pathsMutex sync.Mutex

...

func InitPaths(cfg *Path) error {
    pathsMutex.Lock()
    defer pathsMutex.Unlock()
	return Paths.InitPaths(cfg)
}

```

Callers can also do this on their side, but will generally not think to do so unless they're aware of this issue since this is pretty non-obvious without delving into the implementation, which is why I believe this should be fixed in the library itself.

---

<div class="post-metadata">

**Author:** ![shaunak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shaunak/32/6643_2.png) [@shaunak](https://discuss.elastic.co/u/shaunak)\
**Post date:** [April 18, 2019, 5:56pm UTC](https://discuss.elastic.co/t/initpaths-is-not-threadsafe/177519/2 "2019-04-18T17:56:41Z")

</div>

Thanks for bringing this up @asanderson15 and welcome to the Elastic discussion forums 👋

If you have a GitHub account, would you mind creating an issue in the Beats repository for this: [https://github.com/elastic/beats/issues/new/choose](https://github.com/elastic/beats/issues/new/choose)? That would be a better place to get Beats' developers attention to this technical implementation issue. If you don't have a GitHub account let me know and I'll create the issue on your behalf.

Thanks,

Shaunak

---

<div class="post-metadata">

**Author:** ![asanderson15](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asanderson15/32/45053_2.png) [@asanderson15](https://discuss.elastic.co/u/asanderson15)\
**Post date:** [April 18, 2019, 6:12pm UTC](https://discuss.elastic.co/t/initpaths-is-not-threadsafe/177519/3 "2019-04-18T18:12:58Z")

</div>

Filed: [https://github.com/elastic/beats/issues/11869](https://github.com/elastic/beats/issues/11869).

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 16, 2019, 8:13pm UTC](https://discuss.elastic.co/t/initpaths-is-not-threadsafe/177519/4 "2019-05-16T20:13:00Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
