evcc icon indicating copy to clipboard operation
evcc copied to clipboard

When dsmr is not available log is flooded, backoff not working?

Open vinc-vo opened this issue 3 years ago • 5 comments

Describe the bug

When starting evcc while the dsmr meter is not connected, EVCC starts polling the dsmr meter in an infinite loop, filling up the logs and generating 100% cpu usage. It seems that there is something wrong with the exponential backoff. (Maybe just for me? Could I be missing a parameter? I built from source, but did not edit anything.)

The debug output shows me starting EVCC for a couple of seconds with loglevel debug and no dsmr meter connected.

It feels like the same or a similar issue as was (not?) fixed by "DSMR: prevent busy loop on EOF (https://github.com/evcc-io/evcc/pull/5641)"

I'm using ser2net to convert the usb dsmr serial input, but I believe this issue could occur with a network dsmr adapter as well. Not sure, maybe it is the way ser2net behaves when the serial input is not found.

Steps to reproduce

  1. Set up ser2net (does not matter that the serial device does not exist, that's the point)
  2. Start EVCC
  3. Log fills up very fast, cpu at 100%

ser2net.conf: 2001:raw:600:/dev/tty-DSMR_1:115200 NONE 1STOPBIT 8DATABITS XONXOFF LOCAL -RTSCTS

Configuration details

# open evcc at http://evcc.local:7070
network:
  schema: http
  host: evcc.local # .local suffix announces the hostname on MDNS
  port: 7070

log: debug
levels:
  cache: error

# unique installation id
plant: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

interval: 10s # control cycle interval

meters:
- type: template
  template: dsmr 
  usage: grid  
  host: localhost  
  port: 2001  
  energy: 1-0:1.7.0
  name: grid1
- type: template
  template: solaredge-inverter 
  id: 1  
  host: 192.168.10.99
  port: 1502  
  usage: pv  
  modbus: tcpip  
  timeout: 10s  
  name: pv2

chargers:
- type: template
  template: abl 
  id: 1  
  device: /dev/tty-RS485_1  
  baudrate: 38400  
  comset: 8E1  
  modbus: rs485serial  
  name: wallbox3

loadpoints:
- title: Oprit
  charger: wallbox3
  mode: now
  phases: 3
  mincurrent: 6
  maxcurrent: 32
  resetOnDisconnect: false
 # vehicle: ev1 # set default vehicle (disables vehicle detection)

vehicles:
- type: template
  template: bmw
  title: X3 xDrive 30e
  user: [email protected]
  password: xxxxxxxxxxxxxxxxxxxx
  vin: xxxxxxxxxxxxxxxxx
  capacity: 11.15
  phases: 1
  cache: 15m
  name: ev1

site:
  title: Goorxxxxxxxxxt 5
  meters:
    grid: grid1
    pvs:
    - pv2

Log details

openhabian@openHABianDevice:~ $ sudo journalctl -fau evcc
-- Logs begin at Wed 2023-06-21 11:39:24 CEST. --
Jun 21 11:52:14 openHABianDevice systemd[1]: Started evcc.
Jun 21 11:52:15 openHABianDevice evcc[9282]: [main  ] INFO 2023/06/21 11:52:15 evcc 0.118.1 (b2b4483b)
Jun 21 11:52:15 openHABianDevice evcc[9282]: [main  ] INFO 2023/06/21 11:52:15 using config file: /etc/evcc.yaml
Jun 21 11:52:15 openHABianDevice evcc[9282]: [main  ] INFO 2023/06/21 11:52:15 starting ui and api at :7070
Jun 21 11:52:15 openHABianDevice evcc[9282]: [db    ] INFO 2023/06/21 11:52:15 using sqlite database: /root/.evcc/evcc.db
Jun 21 11:52:15 openHABianDevice evcc[9282]: [dsmr  ] ERROR 2023/06/21 11:52:15 peek: EOF
Jun 21 11:52:15 openHABianDevice evcc[9282]: [dsmr  ] ERROR 2023/06/21 11:52:15 peek: EOF

[a lot of the same lines, you get the point]

Jun 21 11:52:24 openHABianDevice evcc[9282]: [dsmr  ] ERROR 2023/06/21 11:52:24 peek: EOF
Jun 21 11:52:24 openHABianDevice evcc[9282]: [dsmr  ] ERROR 2023/06/21 11:52:24 peek: EOF
Jun 21 11:52:24 openHABianDevice evcc[9282]: [dsmr  ] ERROR 2023/06/21 11:52:24 peek: EOF
Jun 21 11:52:24 openHABianDevice evcc[9282]: [dsmr  ] ERROR 2023/06/21 11:52:24 peek: EOF
Jun 21 11:52:24 openHABianDevice systemd[1]: Stopping evcc...
Jun 21 11:52:24 openHABianDevice systemd[1]: evcc.service: Main process exited, code=killed, status=15/TERM
Jun 21 11:52:24 openHABianDevice systemd[1]: evcc.service: Succeeded.
Jun 21 11:52:24 openHABianDevice systemd[1]: Stopped evcc.


### What type of operating system are you running?

Linux (openhabian - buster)

### Version

evcc version 0.118.1 (b2b4483b)

vinc-vo avatar Jun 21 '23 10:06 vinc-vo

That shouldn't be possible since we stop if no connection can be made:

conn, err := m.connect()
if err != nil {
	return nil, err
}

Why does the connect work when there's no meter?

andig avatar Jun 21 '23 11:06 andig

The ser2net accepts the incoming connection, but there is nothing to be read. Looking at the code, i think backoff.Reset() might happen too soon. I guess you would want backoff for the other errors too.

Update: I got it fixed by moving backoff.Reset() down in the code and adding this after handle("peek", err):

sleep := backoff.NextBackOff().Truncate(time.Second)
log.DEBUG.Printf("next attempt after: %v", sleep) 
time.Sleep(sleep) 

I'm not a real developer tho, so not sure if this is actually ok. I'll also have to test later if it works with the meter actually attached, since I don't have access at this time.

vinc-vo avatar Jun 21 '23 12:06 vinc-vo

It think being able to read anything from the connection should reset the backoff, hence

	if b, err := reader.Peek(1); err == nil {
		backoff.Reset()

		if string(b) != "/" {
			log.DEBUG.Printf("ignoring garbage character: %c\n", b)
			_, _ = reader.ReadByte()
			continue
		}
	} else {
		handle("peek", err)
		continue
	}

Could you give that a try? Will send PR to merge after your ok.

andig avatar Jun 25 '23 10:06 andig

I'm still getting a flood of [dsmr ] ERROR 2023/06/27 12:58:44 peek: EOF

I think there should be a backoff period after every error.

if b, err := reader.Peek(1); err == nil {
	backoff.Reset()

	if string(b) != "/" {
		log.DEBUG.Printf("ignoring garbage character: %c\n", b)
		_, _ = reader.ReadByte()
		continue
	}
} else {
	handle("peek", err)
	continue
}

should be something like this

if b, err := reader.Peek(1); err == nil {
	backoff.Reset()

	if string(b) != "/" {
		log.DEBUG.Printf("ignoring garbage character: %c\n", b)
		_, _ = reader.ReadByte()
		continue
	}
} else {
	handle("peek", err)
	time.Sleep(backoff.NextBackOff().Truncate(time.Second))
	continue
}

vinc-vo avatar Jun 27 '23 11:06 vinc-vo

Added your suggestion. Does that work now?

andig avatar Jun 29 '23 09:06 andig