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

之前我曾经发过贴说在windows下会无法退出的问题。 #77

Open
leeonix opened this issue Nov 23, 2024 · 1 comment
Open
Assignees

Comments

@leeonix
Copy link

leeonix commented Nov 23, 2024

我大概看了下逻辑。问题出在handleQuit()这个函数上。
在执行了exitPrint()后,执行了:

	if len(result.Failed) != 0 {
		os.Exit(1)
	}
	os.Exit(0)

本来应该是没问题的,可莫名其妙的卡住退出不了了。
但我把这一段,挪到了exitPrint()里呢?
就啥事情都没有了,也能正常退出了。
这里也直接退出,在Windows下就不卡住了。

	if len(result.Success) == 0 && len(result.Failed) == 0 && len(result.Saved) == 0 {
		log.Println("本次运行没有上传文件")
		os.Exit(1)
	}

最后是这样修改的。大家测试一下吧:

// 退出处理
func handleQuit() {
	ch := make(chan os.Signal, 1)
	signal.Notify(ch, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

	select {
	case <-ch:
	case <-quit:
	}

	signal.Stop(ch)
	signal.Reset(os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

	log.Println("收到退出信号,正在退出本程序,请等待")

	if uploadingPart {
		multipartCh <- struct{}{}
		<-multipartCh
	}

	keyboard.Close()
	exitPrint()
}

// 程序退出时打印信息
func exitPrint() {
	defer func() {
		if err := recover(); err != nil {
			log.Printf("exitPrint() error: %v", err)
			// 不保存上传结果
			config.ResultDir = ""
			exitPrint()
		}
	}()

	if len(result.Success) == 0 && len(result.Failed) == 0 && len(result.Saved) == 0 {
		log.Println("本次运行没有上传文件")
		os.Exit(1)
	}

	if config.ResultDir != "" {
		resultFile := filepath.Join(config.ResultDir, getTime()+" result.json")
		log.Printf("上传结果保存在 %s", resultFile)
		data, err := json.MarshalIndent(result, "", "    ")
		checkErr(err)
		err = os.WriteFile(resultFile, data, 0644)
		checkErr(err)
	}

	fmt.Printf("上传成功的文件(%d):\n", len(result.Success))
	for _, s := range result.Success {
		fmt.Println(s)
	}
	fmt.Printf("上传失败的文件(%d):\n", len(result.Failed))
	for _, s := range result.Failed {
		fmt.Println(s)
	}
	fmt.Printf("保存上传进度的文件(%d):\n", len(result.Saved))
	for _, s := range result.Saved {
		fmt.Println(s)
	}
	if len(result.Failed) != 0 {
		os.Exit(1)
	}
	os.Exit(0)
}

@orzogc orzogc self-assigned this Nov 23, 2024
@orzogc
Copy link
Owner

orzogc commented Nov 30, 2024

这样修改只是在运行exitPrint时强制退出程序而已
可能是keyboard.Close()卡住了,麻烦删掉下面这行再试试能不能自然退出 @leeonix

defer keyboard.Close()

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

No branches or pull requests

2 participants