Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start and Stop twice make program panic #40

Open
jtbonhomme opened this issue May 18, 2019 · 3 comments
Open

Start and Stop twice make program panic #40

jtbonhomme opened this issue May 18, 2019 · 3 comments

Comments

@jtbonhomme
Copy link

jtbonhomme commented May 18, 2019

Hello,
I may be missing a point, but it seems that starting and stoping a uiprogress twice make the program panic.

Please find bellow a short test program:

package main

import (
    "fmt"
    "time"

    "github.com/gosuri/uiprogress"
)

func createbar() {
    uiprogress.Start()
    defer uiprogress.Stop()
    // create a new bar and prepend the task progress to the bar
    count := 100
    bar := uiprogress.AddBar(count).AppendCompleted().PrependElapsed()
    bar.PrependFunc(func(b *uiprogress.Bar) string {
        return fmt.Sprintf("Task (%d/%d)", b.Current(), count)
    })

    for i := 0; i < count; i++ {
            bar.Incr()
            time.Sleep(time.Millisecond * time.Duration(50))
    }
    time.Sleep(time.Millisecond * time.Duration(500))
}

func main() {
  createbar()
  createbar()
}

The console:

$ go run main.go
Task (100/100)    5s [====================================================================] 100%
Task (1/100)    0s [--------------------------------------------------------------------]   1%
panic: close of closed channel

goroutine 20 [running]:
github.com/gosuri/uiprogress.(*Progress).Listen(0xc00007a1e0)
	/Users/jtbonhomme/Developments/golang/src/github.com/gosuri/uiprogress/progress.go:117 +0x12f
created by github.com/gosuri/uiprogress.(*Progress).Start
	/Users/jtbonhomme/Developments/golang/src/github.com/gosuri/uiprogress/progress.go:134 +0x3f
exit status 2
@danlamanna
Copy link

I'm also experiencing this issue.

@Che4ter
Copy link

Che4ter commented Mar 30, 2020

I have the same issue..

@ubiuser
Copy link

ubiuser commented Oct 19, 2020

I don't think this is a bug, but it definitely needs an example to document this use case for clarity.

Here is what's happening:
uiprogress.Start() and uiprogress.Stop() are using defaultProgress which is initialised with New(), thus defaultProgress is a Progress object that has a tdone channel. In Go, once a channel is closed (calling Stop() in this case) it cannot be reopened or closed again.

Solution:
A new Progress instance must be created with New and call Start/Stop on this object.

package main

import (
    "fmt"
    "time"

    "github.com/gosuri/uiprogress"
)

func createbar() {
    uip := uiprogress.New()
    uip.Start()
    defer uip.Stop()
    // create a new bar and prepend the task progress to the bar
    count := 100
    bar := uip.AddBar(count).AppendCompleted().PrependElapsed()
    bar.PrependFunc(func(b *uiprogress.Bar) string {
        return fmt.Sprintf("Task (%d/%d)", b.Current(), count)
    })

    for i := 0; i < count; i++ {
        bar.Incr()
        time.Sleep(time.Millisecond * time.Duration(50))
    }
    time.Sleep(time.Millisecond * time.Duration(500))
}

func main() {
    createbar()
    createbar()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants