Bug #97824 NullPointerException in NativeProtocol
Submitted: 28 Nov 2019 7:42
Reporter: Roman Leventov Email Updates:
Status: Open Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:8.0.18 OS:Mac OS X
Assigned to: CPU Architecture:Any

[28 Nov 2019 7:42] Roman Leventov
Description:
There may be a NullPointerException in NativeProtocol.java in line 661:

        } finally {
            if (timeoutMillis != 0) {
                try {
                    this.socketConnection.getMysqlSocket().setSoTimeout(oldTimeout); // <- this line
                } catch (IOException e) {
                    throw ExceptionFactory.createCommunicationsException(this.propertySet, this.serverSession, this.getPacketSentTimeHolder(),
                            this.getPacketReceivedTimeHolder(), e, getExceptionInterceptor());
                }
            }
        }

this.socketConnection.getMysqlSocket() may return null.

How to repeat:
    private static Network network;
    private static MySQLContainer mysql;
    private static int mysqlPort;
    private static ToxiproxyContainer toxiproxy;
    private static ToxiproxyContainer.ContainerProxy proxy;
    private static String jdbcUrl;

    @BeforeClass
    public static void setupContainers() {
        network = Network.newNetwork();
        mysql = (MySQLContainer) new MySQLContainer("mysql:5.7").withNetwork(network);
        mysql.start();
        mysqlPort = mysql.getMappedPort(MySQLContainer.MYSQL_PORT);
        toxiproxy = new ToxiproxyContainer().withNetwork(network);
        toxiproxy.start();
        proxy = toxiproxy.getProxy(mysql, MySQLContainer.MYSQL_PORT);
        jdbcUrl = "jdbc:mysql://" + proxy.getContainerIpAddress() + ":" + proxy.getProxyPort() +
                "/" + mysql.getDatabaseName();
    }

    @AfterClass
    public static void shutdownContainers() {
        //noinspection EmptyTryBlock: let try-with-resources do the work
        try (final Network ignore1 = network;
             final MySQLContainer ignore2 = mysql;
             final ToxiproxyContainer ignore3 = toxiproxy) {
        }
    }

    @Test
    public void testHikariDbDisconnected() throws SQLException {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(jdbcUrl);
        config.setUsername(mysql.getUsername());
        config.setPassword(mysql.getPassword());
        try (HikariDataSource ds = new HikariDataSource(config)) {

            proxy.setConnectionCut(true); // Emulate db disconnection

            System.out.println(ds.getConnection().prepareCall("SELECT 1;").execute());
        }
    }