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

echo.Reverse can have nondeterministic output #1237

Closed
3 tasks done
djmally opened this issue Dec 8, 2018 · 3 comments · May be fixed by #1893
Closed
3 tasks done

echo.Reverse can have nondeterministic output #1237

djmally opened this issue Dec 8, 2018 · 3 comments · May be fixed by #1893
Labels

Comments

@djmally
Copy link

djmally commented Dec 8, 2018

Issue Description

With echo v3.3.8 (and probably other versions), it's possible for echo.Reverse to return nondeterministic results due to its implementation. If you define two routes that both map to the same handler function, multiple calls to echo.Reverse on the handler function can result in different outputs.

Checklist

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

Expected behaviour

Calling echo.Reverse on a handler function always results in the same output, whether that is all of the routes that map to a given handler, or the first one found.

Actual behaviour

Calling echo.Reverse on a handler function can result in different outputs each time it is called.

Steps to reproduce

Create a handler function and two routes that map to it (method is irrelevant, since echo.Reverse does not use it). Call echo.Reverse on the handler function many times in a loop and observe that the output may change. See code below as well. This happens because echo.Reverse iterates a map[string]*Route to find a match, and iterating a map in Go produces elements of the map in an arbitrary order.

Working code to debug

package main

import (
	"fmt"
	"reflect"
	"runtime"

	"github.com/labstack/echo"
)

func handler(ctxt echo.Context) error {
	return nil
}

func main() {
	ech := echo.New()
	ech.GET("/abc", handler)
	ech.GET("/xyz", handler)

	for i := 0; i < 100; i++ {
		fmt.Println(ech.Reverse(runtime.FuncForPC(reflect.ValueOf(handler).Pointer()).Name()))
	}
}

Version/commit

I am using version =3.3.8

@stale
Copy link

stale bot commented Feb 6, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Feb 6, 2019
@alexaandru
Copy link
Contributor

Not sure we can do much about this. We should change the way we associate names with routes, which I'm pretty sure it would be a backward incompatible change. Maybe we can improve the documentation instead to list the limitations of this function (if they aren't mentioned already) ? Thoughts @im-kulikov @vishr ?

@stale stale bot removed the wontfix label Feb 9, 2019
@stale
Copy link

stale bot commented Apr 10, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

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

Successfully merging a pull request may close this issue.

2 participants