0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sync.MutexとDIコンテナを使う時の注意点

0
Last updated at Posted at 2017-08-18

実際にやってしまったので戒めのためにメモしておきます。

DIコンテナで生成されるstructはシステムグローバルなシングルトンとして扱われ、2つ以上のリクエストをまたいで同じオブジェクトが参照される。つまり、DIコンテナの内部では2回目にstructを生成する時は、1回目に生成したstructをそのまま引き渡している。

もし、プロパティに「sync.Mutex」を持ったstructがDIコンテナによって生成され、同時に2つのプロセスがそのstructを参照すると、1つのsync.Mutexを共有して使ってしまうことになるので、片方のプロセスがMutex.Block()したまま処理を終了すると、もう片方のプロセスのMutex.Block()が永遠にBlockしたままになってしまう。
これは「sync.WaitGroup」でも同じ危険性がある。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?