Cancel lock operation in Go

I have a blocking operation in a third-party library that I do not control. This could potentially go away forever. So I want to set a timeout on it. The obvious way is to wrap it with a channel and goroutine, and then select the result using time.After . However, the problem is that the goroutine performing the blocking operation could potentially go away forever.

Here is an example to illustrate this http://repl.it/90o

Is there any way to discard goroutine or collect garbage?

+5
source share
2 answers

You cannot stop goroutine from the outside. Gorotin should support some signaling about completion (most often the channel). But if it is not, you cannot force him or kill him.

If you can’t do anything about what you are using, you can use it in another process (in another application launched by your application) that you can kill, but it's just ugly and too cumbersome.

+3
source

I don’t think you can do anything. In Go, you cannot stop goroutine outside. If you cannot fix this library, and you think that it can be blocked forever, then write a new one that you can control.

0
source

Source: https://habr.com/ru/post/1212285/


All Articles