vapor / mysql-kit

🐬 Pure Swift MySQL client built on non-blocking, event-driven sockets.
MIT License
222 stars 74 forks source link

Heroku dyno crash using MySQL #100

Closed O-mkar closed 7 years ago

O-mkar commented 7 years ago

I recently migrated to Vapor 2.0 and I'm facing this crash on Heroku on Mac its working fine but on Heroku its crashing when to try to connect to MySQL

   drop.get("make") { req in
            mysql = try MySQL.Database.makeDB().makeConnection()
            let mysql_CHECKDB = try MySQL.Database.makeDB().makeConnection()
            return req.description
        }

When i request make it crashes

screen shot 2017-06-17 at 10 00 55 pm

extension MySQL.Database {
    static func makeDB() -> MySQL.Database {
        do {
            let mysql = try MySQL.Database(
                hostname: "127.0.0.1",
               user: "root",
               password: "",
                database: "Radar"
            )

            try mysql.makeConnection().execute("SELECT @@version")

            return mysql
        } catch {

            fatalError("MySQL Error")
        }
    }
}
vzsg commented 7 years ago

I'm pretty sure you don't have a MySQL server at 127.0.0.1 when you deploy your application to Heroku. You might want to revisit that configuration – preferably by using environment variables...

O-mkar commented 7 years ago

@vzsg Yeah I'm using production credentials, I don't know why it is crashing. On my local system it works fine with live credentials.

O-mkar commented 7 years ago

@vzag I guess it's the same issue which is affecting my code https://github.com/vapor/vapor/issues/954 But mine at least build successfully


-----> Swift app detected
Cloning into 'swiftenv'...
Swift 3 Heroku Installer
????  Version: 3.1.1
????  Operating System: ubuntu1404
???? Installing Swiftenv
Cloning into '/app/.swiftenv'...
???? Installing Swift
Downloading https://swift.org/builds/swift-3.1.1-release/ubuntu1604/swift-3.1.1-RELEASE/swift-3.1.1-RELEASE-ubuntu16.04.tar.gz
/tmp/swiftenv-3.1.1- /tmp/build_39b7750742f2d091730e07c9a9afe3fc
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
/tmp/build_39b7750742f2d091730e07c9a9afe3fc
3.1.1 has been installed.
???  Done
precompile
-----> Building Package ... this will take a while
Fetching https://github.com/vapor/vapor.git
Fetching https://github.com/BrettRToomey/Jobs.git
Fetching https://github.com/vapor/mysql.git
Fetching https://github.com/vapor/validation-provider.git
Fetching https://github.com/vapor/cmysql.git
Fetching https://github.com/vapor/leaf-provider.git
Fetching https://github.com/vapor/console.git
Fetching https://github.com/vapor/multipart.git
Fetching https://github.com/vapor/debugging.git
Fetching https://github.com/vapor/mysql-driver.git
Fetching https://github.com/vapor/routing.git
Fetching https://github.com/vapor/bits.git
Fetching https://github.com/vapor/sqlite.git
Fetching https://github.com/vapor/random.git
Fetching https://github.com/vapor/node.git
Fetching https://github.com/vapor/core.git
Fetching https://github.com/vapor/ctls.git
Fetching https://github.com/vapor/engine.git
Fetching https://github.com/vapor/sockets.git
Fetching https://github.com/vapor/fluent.git
Fetching https://github.com/vapor/json.git
Fetching https://github.com/vapor/crypto.git
Fetching https://github.com/vapor/tls.git
Fetching https://github.com/vapor/validation.git
Fetching https://github.com/vapor/fluent-provider.git
Fetching https://github.com/vapor/bcrypt.git
Fetching https://github.com/vapor/leaf.git
Cloning https://github.com/vapor/leaf.git
Resolving https://github.com/vapor/leaf.git at 2.0.1
Cloning https://github.com/vapor/routing.git
Resolving https://github.com/vapor/routing.git at 2.0.0
Cloning https://github.com/vapor/mysql-driver.git
Resolving https://github.com/vapor/mysql-driver.git at 2.0.1
Cloning https://github.com/vapor/sqlite.git
Resolving https://github.com/vapor/sqlite.git at 2.0.0
Cloning https://github.com/vapor/json.git
Resolving https://github.com/vapor/json.git at 2.0.2
Cloning https://github.com/vapor/random.git
Resolving https://github.com/vapor/random.git at 1.0.0
Cloning https://github.com/vapor/vapor.git
Resolving https://github.com/vapor/vapor.git at 2.1.0
Cloning https://github.com/vapor/engine.git
Resolving https://github.com/vapor/engine.git at 2.0.4
Cloning https://github.com/vapor/crypto.git
Resolving https://github.com/vapor/crypto.git at 2.0.0
Cloning https://github.com/vapor/debugging.git
Resolving https://github.com/vapor/debugging.git at 1.0.0
Cloning https://github.com/vapor/tls.git
Resolving https://github.com/vapor/tls.git at 2.0.4
Cloning https://github.com/vapor/core.git
Resolving https://github.com/vapor/core.git at 2.0.2
Cloning https://github.com/vapor/console.git
Resolving https://github.com/vapor/console.git at 2.1.0
Cloning https://github.com/vapor/sockets.git
Resolving https://github.com/vapor/sockets.git at 2.0.1
Cloning https://github.com/vapor/bcrypt.git
Resolving https://github.com/vapor/bcrypt.git at 1.0.0
Cloning https://github.com/vapor/leaf-provider.git
Resolving https://github.com/vapor/leaf-provider.git at 1.0.0
Cloning https://github.com/vapor/mysql.git
Resolving https://github.com/vapor/mysql.git at 2.0.1
Cloning https://github.com/vapor/node.git
Resolving https://github.com/vapor/node.git at 2.0.3
Cloning https://github.com/vapor/cmysql.git
Resolving https://github.com/vapor/cmysql.git at 2.0.2
Cloning https://github.com/vapor/validation.git
Resolving https://github.com/vapor/validation.git at 1.0.1
Cloning https://github.com/BrettRToomey/Jobs.git
Resolving https://github.com/BrettRToomey/Jobs.git at 1.0.0
Cloning https://github.com/vapor/multipart.git
Resolving https://github.com/vapor/multipart.git at 2.0.0
Cloning https://github.com/vapor/fluent-provider.git
Resolving https://github.com/vapor/fluent-provider.git at 1.1.1
Cloning https://github.com/vapor/bits.git
Resolving https://github.com/vapor/bits.git at 1.0.0
Cloning https://github.com/vapor/validation-provider.git
Resolving https://github.com/vapor/validation-provider.git at 1.1.0
Cloning https://github.com/vapor/ctls.git
Resolving https://github.com/vapor/ctls.git at 1.0.0
Cloning https://github.com/vapor/fluent.git
Resolving https://github.com/vapor/fluent.git at 2.1.0
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install cmysql using your system-packager:
    apt-get install cmysql
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install cmysql using your system-packager:
    apt-get install cmysql
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install cmysql using your system-packager:
    apt-get install cmysql
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install cmysql using your system-packager:
    apt-get install cmysql
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install cmysql using your system-packager:
    apt-get install cmysql
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
note: you may be able to install cmysql using your system-packager:
    apt-get install cmysql
note: you may be able to install ctls using your system-packager:
    apt-get install ctls
Compile Swift Module 'Debugging' (1 sources)
Compile Swift Module 'Bits' (19 sources)
Compile Swift Module 'Jobs' (6 sources)
Compile Swift Module 'Validation' (15 sources)
Compile Swift Module 'libc' (1 sources)
Compile Swift Module 'Core' (23 sources)
Compile Swift Module 'Transport' (10 sources)
Compile Swift Module 'PathIndexable' (2 sources)
Compile Swift Module 'Console' (35 sources)
Compile Swift Module 'Random' (6 sources)
Compile Swift Module 'Node' (38 sources)
Compile Swift Module 'Crypto' (13 sources)
Compile Swift Module 'BCrypt' (9 sources)
Compile Swift Module 'Sockets' (22 sources)
Compile Swift Module 'JSON' (9 sources)
Compile Swift Module 'Leaf' (33 sources)
Compile Swift Module 'TLS' (12 sources)
Compile Swift Module 'MySQL' (11 sources)
Compile CHTTP http_parser.c
Compile Swift Module 'SMTP' (21 sources)
Linking CHTTP
Compile Swift Module 'URI' (6 sources)
Compile Swift Module 'HTTP' (42 sources)
Compile Swift Module 'Cookies' (11 sources)
Compile Swift Module 'WebSockets' (14 sources)
Compile Swift Module 'Multipart' (6 sources)
Compile Swift Module 'Branches' (3 sources)
Compile Swift Module 'Routing' (10 sources)
Compile Swift Module 'FormData' (4 sources)
Compile Swift Module 'Cache' (2 sources)
Compile Swift Module 'Configs' (13 sources)
Compile Swift Module 'Sessions' (7 sources)
Compile Swift Module 'Vapor' (97 sources)
Compile Swift Module 'Testing' (6 sources)
Compile Swift Module 'VaporValidation' (2 sources)
Compile Swift Module 'LeafProvider' (4 sources)
Compile Swift Module 'App' (4 sources)
/tmp/build_39b7750742f2d091730e07c9a9afe3fc/Sources/App/Routes.swift:20:17: warning: initialization of immutable value 'mysql_CHECKDB' was never used; consider replacing with assignment to '_' or removing it
            let mysql_CHECKDB = try MySQL.Database.makeDB().makeConnection()
            ~~~~^~~~~~~~~~~~~
            _
/tmp/build_39b7750742f2d091730e07c9a9afe3fc/Sources/App/Routes.swift:23:13: warning: initialization of immutable value 'log' was never used; consider replacing with assignment to '_' or removing it
        let log = self.log.self
        ~~~~^~~
        _
/tmp/build_39b7750742f2d091730e07c9a9afe3fc/Sources/App/Routes.swift:24:13: warning: initialization of immutable value 'unauthorizedMessage' was never used; consider replacing with assignment to '_' or removing it
        let unauthorizedMessage = "Unauthorized: Access is denied"
        ~~~~^~~~~~~~~~~~~~~~~~~
        _
Compile Swift Module 'Run' (1 sources)
Linking ./.build/release/Run
-----> Installing dynamic libraries
-----> Installing binaries
-----> Discovering process types
       Procfile declares types -> web
-----> Compressing...
       Done: 72.2M
-----> Launching...
       Released v13
       https://[URL].herokuapp.com/ deployed to Heroku
vzsg commented 7 years ago

It's definitely not related, as that issue was about being unable to compile the project (fortunately it was resolved).

In the app log, it says "Process exited with status 139", which is the code for segmentation fault (128 + 11). This can be an issue in a C component (unlikely now) or the result of that fatalError call...

Do everyone a favor, and before killing the app with that call, use drop.log to log the actual error message. Something like drop.log.error("MySQL Error: \(error)").

(The Swift built-in print function is not enough, because its output is buffered, an in case of a crash, you will not see anything. Just like now.)

O-mkar commented 7 years ago

@vzsg check this #101 and drop.log.error("MySQL Error: (error)") is not working

O-mkar commented 7 years ago

@vzsg i was able to get crash log for you

screen shot 2017-06-18 at 6 13 09 pm
vzsg commented 7 years ago

So it's apparent that there's a connection error (or rather a "connection lost" error) – which is problematic of course, but the only reason it crashes is because you call fatalError. Don't do that, Swift isn't PHP.

Instead, throw the error again. Or just remove the whole do-try pair, so the error can bubble up.


Once you did this, the real question becomes: how much of the requests fail? And what kind of MySQL is this? An addon?

O-mkar commented 7 years ago

@vzsg its an addon I use clear DB if I make multiple connections it crash it happens, again and again, I have added code to #101 to reproduce a the bug.