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 doesn't work with r2d2-diesel #726

Closed
kardeiz opened this Issue Feb 17, 2017 · 1 comment

Comments

Projects
None yet
1 participant
@kardeiz
Contributor

kardeiz commented Feb 17, 2017

MysqlConnection can't be shared between threads, which makes it impossible to use in r2d2-diesel (or presumably any multi-thread environment). This is in diesel 0.11.0.

My code looks like:

fn connection_pool() -> ::err::Result<Pool> {
    let url = &::utils::config::CONFIG.db.url;
    let config = ::r2d2::Config::default();
    let manager = ConnectionManager::<MysqlConnection>::new(url);
    Ok(::r2d2::Pool::new(config, manager)?)
}

which fails to compile with (truncated):

within `diesel::mysql::connection::stmt::Statement`, 
  the trait `std::marker::Send` is not implemented for `*mut mysqlclient_sys::st_mysql_stmt`
note: `*mut mysqlclient_sys::st_mysql_stmt` cannot be sent between threads safely

I don't know if just adding unsafe impl Send for MysqlConnection {} would fix it (if might not be safe or even compile).

sgrif added a commit that referenced this issue Feb 17, 2017

Ensure that `MysqlConnection` is `Send`
I've also just made it a supertrait of `Connection` so I don't make this
mistake again.

Fixes #726.

sgrif added a commit that referenced this issue Feb 17, 2017

Ensure that `MysqlConnection` is `Send`
`mysql::Statement` is not send due to the raw pointer. The `MYSQL_STMT`
type is very much unsafe to be sent across threads, since it contains a
pointer to the connection which is not thread safe. However, we never
expose statements directly, and sending the whole connection is safe.

I've also just made it a supertrait of `Connection` so I don't make this
mistake again.

Fixes #726.
@kardeiz

This comment has been minimized.

Contributor

kardeiz commented Feb 17, 2017

Cool, thanks!

@kardeiz kardeiz closed this Feb 17, 2017

sgrif added a commit that referenced this issue Feb 17, 2017

Ensure that `MysqlConnection` is `Send`
`mysql::Statement` is not send due to the raw pointer. The `MYSQL_STMT`
type is very much unsafe to be sent across threads, since it contains a
pointer to the connection which is not thread safe. However, we never
expose statements directly, and sending the whole connection is safe.

I've also just made it a supertrait of `Connection` so I don't make this
mistake again.

Fixes #726.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment