Skip to content
This repository has been archived by the owner on Apr 2, 2023. It is now read-only.

Intermittent "Slice bounds out of range" during decoding #40

Open
nathanKramer opened this issue Aug 25, 2020 · 5 comments
Open

Intermittent "Slice bounds out of range" during decoding #40

nathanKramer opened this issue Aug 25, 2020 · 5 comments
Labels

Comments

@nathanKramer
Copy link

nathanKramer commented Aug 25, 2020

I have noticed an intermittent panic error that occurs during decoding. This happens specifically when seek is called several times in quick succession. (e.g rapidly clicking).
It might be appropriate to debounce seeking, however I thought I'd raise this anyway as I'm not sure.

panic: runtime error: slice bounds out of range [5696:4608]

The panic occurs here:

d.buf = d.buf[d.bytesPerFrame+(d.pos%d.bytesPerFrame):]

I can reproduce this using the following faiface/beep code. It seems possible that this is a bug within beep. I don't understand the situation well enough so I have decided to at least put this on your radar.

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/faiface/beep"
	"github.com/faiface/beep/mp3"
	"github.com/faiface/beep/speaker"
)

var soundFormat *beep.Format
var musicStreamer *beep.StreamSeekCloser

func prepareStreamer(file string) (*beep.StreamSeekCloser, *beep.Format) {
	sound, _ := os.Open(file)
	streamer, format, err := mp3.Decode(sound)
	if err != nil {
		panic(err)
	}
	return &streamer, &format
}

func main() {
	musicStreamer, soundFormat = prepareStreamer("an.mp3")
	speaker.Init(soundFormat.SampleRate, soundFormat.SampleRate.N(time.Second/10))

	speaker.Clear()
	s := *musicStreamer
	s.Seek(0)
	speaker.Play(s)

	ch := make(chan int)
	go func() {
		for true {
			time.Sleep(250 * time.Millisecond)
			fmt.Println("seeking")
			streamer := *musicStreamer
			streamer.Seek(700000) // anywhere into the song
		}
	}()
	<-ch

	defer s.Close()
}

One way to fix this:

bufLength := int64(len(d.buf))
idx := d.bytesPerFrame + (d.pos % d.bytesPerFrame)
if idx > bufLength {
	idx = bufLength
}

However I don't understand the underlying cause so there may be a better solution.

@hajimehoshi
Copy link
Owner

Thanks. Unfortunately I don't fully understand the implementation details because I just ported the existing MP3 decoder to Go. I'll take a look but I'm not sure I can find a correct fix.

@hajimehoshi hajimehoshi added the bug label Sep 2, 2020
@hajimehoshi
Copy link
Owner

This part seems what I wrote. I'll take a look.

@hajimehoshi
Copy link
Owner

  	streamer.Seek(700000) // anywhere into the song

Isn't this simply out of range? Or, was this error happening with any position?

@nathanKramer
Copy link
Author

nathanKramer commented Sep 3, 2020

Isn't this simply out of range? Or, was this error happening with any position?

No, it wasn't out of range, and yes it seems to happen with any position. It seemed to happen randomly. The code to reproduce is meant to emulate a user clicking once every 250ms, as this was the original way I found the issue.

@evanhyd
Copy link

evanhyd commented Apr 2, 2023

This bug still occurs frequently as of 2023 April. I am trying to change the seeker progress with a GUI slidebar, but it crashes if I send the request too frequently. It is always the message:

panic: runtime error: slice bounds out of range [????:4608]. Notice the slice size is always capped at the magic number 4608

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

No branches or pull requests

3 participants