go-imap icon indicating copy to clipboard operation
go-imap copied to clipboard

Already logged in

Open tantanbei opened this issue 6 years ago • 5 comments

When I repeat do dial and login for more time, it return me "Already logged in"

func LoginEmail(addr, username, password string) (c *client.Client, err error) {
	log.Println("Connecting to server...")

	// Connect to server
	c, err = client.DialTLS(addr, nil)
	if err != nil {
		return
	}

	log.Println("Email Login...")
	// Login
	if err = c.Login(username, password); err != nil {
		return
	}
	return
}

c, err := reciept.LoginEmail(globalContext.Email.Addr, globalContext.Email.Email, globalContext.Email.Password)
if err != nil {
	log.Printf("LoginEmail . err[%s]", err.Error())
	return
}
defer c.Logout()

tantanbei avatar Nov 09 '17 03:11 tantanbei

I also got that problem. My app is running in intervals to connect to multiple accounts. Currently I cant reproduce it but I can see the error-log from go-imap telling me "Already logged in" sometimes.

For sure I used defer client.Logout() right after the line that's logging in. Is the client something like a singleton? Is there no chance to create instances? I'm not very experienced with imap. Maybe this is a socket-problem. Looking forward having an answer in here.

Thanks for your effort.

0000day avatar Nov 09 '17 16:11 0000day

Can you provide logs with debug enabled?

emersion avatar Nov 10 '17 17:11 emersion

It is hard to reproduce, never show again... And if I will get any logs, I will send you. Thanks for your effort.

tantanbei avatar Nov 15 '17 06:11 tantanbei

This can reproduce with these lines:

package main

import (
	"crypto/tls"
	"log"
	"os"

	"github.com/emersion/go-imap"
	"github.com/emersion/go-imap/client"
)

func main() {
	log.Println("Connecting to server...")

	// Connect to server
	c, err := client.Dial("imap.example.com:143")
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Connected")
	c.SetDebug(os.Stdout)

	// Don't forget to logout
	defer c.Logout()

	c.StartTLS(&tls.Config{InsecureSkipVerify: true})
	// Login
	if err := c.Login("[email protected]", "xxx"); err != nil {
		log.Fatal(err)
	}
	log.Println("Logged in")

	log.Println(c.Logout())
	log.Println(c.State() == imap.LogoutState)
	log.Println(c.State() == imap.NotAuthenticatedState)

	// Login
	if err := c.Login("[email protected]", "xxx"); err != nil {
		log.Fatal(err)
	}
	log.Println("Logged in")
	log.Println("Done!")
}

Log:

2018/05/25 17:30:28 Connecting to server...
2018/05/25 17:30:28 Connected
BpzhnQ STARTTLS
O2EBhg LOGIN [email protected] xxx
O2EBhg OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS BINARY MOVE SEARCH=FUZZY SPECIAL-USE QUOTA] Logged in
2018/05/25 17:30:28 Logged in
F_7rhg LOGOUT
* BYE Logging out
F_7rhg OK Logout completed.
2018/05/25 17:30:28 <nil>
2018/05/25 17:30:28 true
2018/05/25 17:30:28 false
2018/05/25 17:30:28 Already logged in
exit status 1

cuonglm avatar May 25 '18 10:05 cuonglm

Logout terminates the connection. See https://tools.ietf.org/html/rfc3501#section-3.4.

Nevertheless, the error reported by go-imap is not correct.

foxcpp avatar May 21 '19 16:05 foxcpp

Closing because this is an issue about go-imap v1. I'm now focusing on go-imap v2.

Please re-open if you can reproduce with go-imap v2.

emersion avatar Apr 04 '23 14:04 emersion