Repository: syncthing/syncthing
File: cmd/syncthing/monitor.go (Go)

Code before:
	if lerr == nil {
		return e, nil
	}
	return "", err
}

func copyStderr(stderr io.Reader, dst io.Writer) {
	br := bufio.NewReader(stderr)

	var panicFd *os.File
	defer func() {
		if panicFd != nil {
			_ = panicFd.Close()
			maybeReportPanics()
		}
	}()

	for {
		line, err := br.ReadString('\n')
		if err != nil {
			return
		}

		dst.Write([]byte(line))

		if panicFd == nil && strings.HasPrefix(line, "panic:") || strings.HasPrefix(line, "fatal error:") {
			panicFd, err = os.Create(locations.GetTimestamped(locations.PanicLog))
			if err != nil {
				l.Warnln("Create panic log:", err)
				continue
			}

			l.Warnf("Panic detected, writing to \"%s\"", panicFd.Name())
			if strings.Contains(line, "leveldb") && strings.Contains(line, "corrupt") {
				l.Warnln(`
*********************************************************************************
* Crash due to corrupt database.                                                *
*                                                                               *
* This crash usually occurs due to one of the following reasons:                *
*  - Syncthing being stopped abruptly (killed/loss of power)                    *
*  - Bad hardware (memory/disk issues)                                          *
*  - Software that affects disk writes (SSD caching software and similar)       *
*                                                                               *
* Please see the following URL for instructions on how to recover:              *
*   https://docs.syncthing.net/users/faq.html#my-syncthing-database-is-corrupt  *
*********************************************************************************
`)
			} else {
				l.Warnln("Please check for existing issues with similar panic message at https://github.com/syncthing/syncthing/issues/")
				l.Warnln("If no issue with similar panic message exists, please create a new issue with the panic log attached")
			}

Review comment (suggestion):
```suggestion
		if panicFd == nil && (strings.HasPrefix(line, "panic:") || strings.HasPrefix(line, "fatal error:")) {
```

Code after:
	if lerr == nil {
		return e, nil
	}
	return "", err
}

func copyStderr(stderr io.Reader, dst io.Writer) {
	br := bufio.NewReader(stderr)

	var panicFd *os.File
	defer func() {
		if panicFd != nil {
			_ = panicFd.Close()
			maybeReportPanics()
		}
	}()

	for {
		line, err := br.ReadString('\n')
		if err != nil {
			return
		}

		dst.Write([]byte(line))

		if panicFd == nil && (strings.HasPrefix(line, "panic:") || strings.HasPrefix(line, "fatal error:")) {
			panicFd, err = os.Create(locations.GetTimestamped(locations.PanicLog))
			if err != nil {
				l.Warnln("Create panic log:", err)
				continue
			}

			l.Warnf("Panic detected, writing to \"%s\"", panicFd.Name())
			if strings.Contains(line, "leveldb") && strings.Contains(line, "corrupt") {
				l.Warnln(`
*********************************************************************************
* Crash due to corrupt database.                                                *
*                                                                               *
* This crash usually occurs due to one of the following reasons:                *
*  - Syncthing being stopped abruptly (killed/loss of power)                    *
*  - Bad hardware (memory/disk issues)                                          *
*  - Software that affects disk writes (SSD caching software and similar)       *
*                                                                               *
* Please see the following URL for instructions on how to recover:              *
*   https://docs.syncthing.net/users/faq.html#my-syncthing-database-is-corrupt  *
*********************************************************************************
`)
			} else {
				l.Warnln("Please check for existing issues with similar panic message at https://github.com/syncthing/syncthing/issues/")
				l.Warnln("If no issue with similar panic message exists, please create a new issue with the panic log attached")
			}