Skip to content

double free or corruption (out) and Aborted (core dumped) #80

@RabbitHareLu

Description

@RabbitHareLu

I'm using Javacan epoll in Spring Boot 3.5.9. When I shut down the Spring application, Spring is supposed to release resources, but it throws the error "double free or corruption (out) Aborted (core dumped)".
After several attempts, I discovered that calling selector.close() within the close method causes this exception to be thrown.
Below is my test code:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tel.schich.javacan.CanChannels;
import tel.schich.javacan.CanSocketOptions;
import tel.schich.javacan.NetworkDevice;
import tel.schich.javacan.RawCanChannel;
import tel.schich.javacan.platform.linux.epoll.EPollSelector;
import tel.schich.javacan.select.SelectorRegistration;

import java.io.IOException;
import java.util.EnumSet;


@Configuration
public class CanConfiguration {

    @Bean
    public EPollSelector ePollSelector() {
        try {
            return EPollSelector.open();
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Bean
    public RawCanChannel rawCanChannel(EPollSelector ePollSelector) {
        try {
            RawCanChannel channel = CanChannels.newRawChannel();
            channel.configureBlocking(false);

            channel.setOption(CanSocketOptions.RECV_OWN_MSGS, false);

            channel.setOption(CanSocketOptions.LOOPBACK, true);

            channel.bind(NetworkDevice.lookup("vcan0"));

            ePollSelector.register(channel, EnumSet.of(SelectorRegistration.Operation.READ));

            return channel;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}








import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import tel.schich.javacan.CanFrame;
import tel.schich.javacan.RawCanChannel;
import tel.schich.javacan.platform.linux.UnixFileDescriptor;
import tel.schich.javacan.platform.linux.epoll.EPollSelector;
import tel.schich.javacan.select.IOEvent;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Component
public class CanService {

    private volatile boolean flag = true;

    @Resource
    public EPollSelector selector;

    @Resource
    public RawCanChannel channel;

    @PostConstruct
    private void init() {
        CompletableFuture.runAsync(() -> {
            try {
                while (flag) {
                    List<IOEvent<UnixFileDescriptor>> events = selector.select();

                    for (IOEvent<UnixFileDescriptor> event : events) {
                        RawCanChannel rawCanChannel = (RawCanChannel) event.getRegistration().getChannel();
                        CanFrame canframe = rawCanChannel.read();
                        log.info("CanFrame: {}", canframe);
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        });
    }

    public void writeCanFrame(CanFrame canframe) {
        try {
            channel.write(canframe);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @PreDestroy
    private void close() {
        flag = false;
        try {
            if (selector.isOpen()) {
                selector.close();
                log.info("selector close");
            }

            if (channel.isOpen()) {
                channel.close();
                log.info("channel close");
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

program close error:
2026-01-14 14:24:38 [INFO ] [SpringApplicationShutdownHook] com.lsl.can.CanService.close(CanService.java:72) - selector close
2026-01-14 14:24:39 [INFO ] [SpringApplicationShutdownHook] com.lsl.can.CanService.close(CanService.java:79) - channel close
double free or corruption (out)
Aborted (core dumped)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions