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

MySQLConnection closes unexpectedly in Swift Command Line Tool #104

Open
huibert7 opened this issue Feb 8, 2024 · 2 comments
Open

MySQLConnection closes unexpectedly in Swift Command Line Tool #104

huibert7 opened this issue Feb 8, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@huibert7
Copy link

huibert7 commented Feb 8, 2024

Describe the bug

MySQLConnection closes unexpectedly in a Swift Command Line Tool that I am writing.

To Reproduce

import Foundation
import MySQLNIO

@main
struct InsertValuesIntoMySQLDB {
    
    static func main() {
        do {
            try insertRecords()
        } catch {
            print("There was an error!")
            print(error.localizedDescription)
        }
    }
    
    static func insertRecords() throws {
        let addr = try SocketAddress.makeAddressResolvingHost(
            "database.domain.com",
            port: 3306
        )
        let eventGroup = MultiThreadedEventLoopGroup(numberOfThreads: 4)
        let eventLoop = eventGroup.next()
        let conn = try MySQLConnection.connect(to: addr, username: "admin", database: "quora", password: "xyzzy", tlsConfiguration: nil, on: eventLoop).wait()
        defer {
            do {
                try conn.close().wait()
            } catch {
                print("Error: \(error)")
            }
        }
        let rows = try conn.simpleQuery("SELECT COUNT(*) from questions").wait()
        print("rows: \(rows.count)")
//      try eventGroup.syncShutdownGracefully()
    }
}

Steps to reproduce the behavior:
When I run the code I get:
rows: 0 (should be > 0)
Error: alreadyClosed

The query clearly reaches the database server since a misspelled table name generates an error (MySQL error: Server error: Table 'quora.question' doesn't exist), but when the SQL is valid, I get no response.

If I uncomment the last line (try eventGroup.syncShutdownGracefully), then I get the following error:

ERROR: Cannot schedule tasks on an EventLoop that has already shut down. This will be upgraded to a forced crash in future SwiftNIO versions.

Environment

  • OS version: macOS Sonoma 14.3

This is my first time trying to use mysql-nio as well as swift-no, therefore I could be totally wrong on how to use these packages, but I have run out of ideas. Thanks in advance for any kind of help you could provide.

@huibert7 huibert7 added the bug Something isn't working label Feb 8, 2024
@0xTim
Copy link
Member

0xTim commented Mar 25, 2024

@huibert7 sorry for the delay. There are a few things that you should improve with your code to start debugging this.

First I strongly suggest you start using the async APIs (and using .get() where these aren't available. That will avoid any issues around waiting on event loops. Second, you're creating a new event loop group on every insert which is a bad idea. You should ideally use the singleton event loop group that's managed for you.

Those 2 suggestions might fix your issue, but feel free to post if it doesn't help or produces a different error

@huibert7
Copy link
Author

Thanks for your help, Tim. I will try your suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants