Skip to content

Instantly share code, notes, and snippets.

@pts-owentran
Created July 13, 2017 07:13
Show Gist options
  • Save pts-owentran/5aac9574dfc9684f47250c0df08f8d29 to your computer and use it in GitHub Desktop.
Save pts-owentran/5aac9574dfc9684f47250c0df08f8d29 to your computer and use it in GitHub Desktop.
You can throw this into lib/core_ext in your rails app to prevent implicit connections being made with a stack trace to track down the errant code.
######################
#
# This is hacked from the original code for Rails 4.1. Owen went in and hacked the ConnectionPool
# so it's not fool proof but prevents ShopHotelsCommand from grabbing dozens of idle DB connections.
#
# If you create a thread yourself, if it uses ActiveRecord objects without
# explicitly checking out a connection, one will still be checked out implicitly.
# If it is never checked back in with `ActiveRecord::Base.clear_active_connections!`,
# then it will be leaked.
#
# For some uses, we want to avoid being able to do that kind of implicit checkout,
# force all ActiveRecord use to be via an explicit checkout using with_connection
# or checkout.
#
# With this monkey patch, a thread can call:
#
# ActiveRecord::Base.forbid_implicit_checkout_for_thread!
#
# And subsequently, if that thread accidentally tries to do an implicit
# checkout, an exception will be raised.
#
# The exception raised is defined here as ActiveRecord::ImplicitConnectionForbiddenError < ActiveRecord::ConnectionTimeoutError
#
##########################
module ActiveRecord
class Base
class << self
def forbid_implicit_checkout_for_thread!
Thread.current[:active_record_forbid_implicit_connections] = true
end
def permit_implicit_checkout_for_thread!
Thread.current[:active_record_forbid_implicit_connections] = false
end
def implicit_checkout_for_thread_forbidden?
Thread.current[:active_record_forbid_implicit_connections] == true
end
end
end
module ForbidImplicitConnection
def connection
if Thread.current[:active_record_forbid_implicit_connections]
msg = "Implicit ActiveRecord checkout attempted when Thread :active_record_forbid_implicit_connections set!"
backtrace = Thread.current.backtrace.join("\n")
# I want to make SURE I see this error in test output, even though
# in some cases my code is swallowing the exception.
if Rails.env.test?
$stderr.puts msg
$stderr.puts backtrace
end
raise ActiveRecord::ImplicitConnectionForbiddenError, msg
end
super
end
end
# We're refusing to give a connection when asked for. Same outcome
# as if the pool timed out on checkout, so let's subclass the exception
# used for that.
ImplicitConnectionForbiddenError = Class.new(::ActiveRecord::ConnectionTimeoutError)
end
ActiveRecord::ConnectionAdapters::ConnectionPool.send(:prepend, ActiveRecord::ForbidImplicitConnection)
@Rovel
Copy link

Rovel commented Apr 4, 2022

Keybase proof

I hereby claim:

* I am angadjawad on github.

* I am anagda (https://keybase.io/anagda) on keybase.

* I have a public key ASAAAk7vwhggJp_QcqxA7Wz43fTZ02q0smgtQ_TqgyjSwQo

To claim this, I am signing this object:

{
  "body": {
    "key": {
      "eldest_kid": "012000024eefc21820269fd072ac40ed6cf8ddf4d9d36ab4b2682d43f4ea8328d2c10a",
      "host": "keybase.io",
      "kid": "012000024eefc21820269fd072ac40ed6cf8ddf4d9d36ab4b2682d43f4ea8328d2c10a",
      "uid": "9443adc40b518ff4b3e53b4251716519",
      "username": "anagda"
    },
    "merkle_root": {
      "ctime": 1591917845,
      "hash": "84abf11b027bffbf239c52ae6de641abf010030a3687f6b8217d3261824801e23708b5a5ba2ed850f11f8d1053a960f88dfb33907897e34b7bbfcdd3082265d2",
      "hash_meta": "acdf0cf26a0cb004e4bb1adab8a8cedba5ab6a0160bc7ff4d5edceb759e57c16",
      "seqno": 16637179
    },
    "service": {
      "entropy": "4FQbb7d6yUDPM3aIfyqtTQRf",
      "name": "github",
      "username": "angadjawad"
    },
    "type": "web_service_binding",
    "version": 2
  },
  "client": {
    "name": "keybase.io go client",
    "version": "5.5.0"
  },
  "ctime": 1591917886,
  "expire_in": 504576000,
  "prev": "f733c6efeb893bce7a6ee46d4676265dbc1927e18057f5d18d4eae12ab57b1db",
  "seqno": 27,
  "tag": "signature"
}

with the key ASAAAk7vwhggJp_QcqxA7Wz43fTZ02q0smgtQ_TqgyjSwQo, yielding the signature:

hKRib2R5hqhkZXRhY2hlZMOpaGFzaF90eXBlCqNrZXnEIwEgAAJO78IYICaf0HKsQO1s+N302dNqtLJoLUP06oMo0sEKp3BheWxvYWTESpcCG8Qg9zPG7+uJO856buRtRnYmXbwZJ+GAV/XRjU6uEqtXsdvEIO/qlGQlF49NE9AYzvtbBp5gWnIMmiwCtc+xvotZqGseAgHCo3NpZ8RAA12ZKIo6CxQKdCp6lQgIpjgxIctAK5mf8h0d0q74vwQWUrQift7qD3UR6KurBMt5IoR1qKSxIfjxxxQE6QNoB6hzaWdfdHlwZSCkaGFzaIKkdHlwZQildmFsdWXEINegvzJJdNnvpBPTnu4UwD0m3mxqvmUrMCnnO7DbtXtPo3RhZ80CAqd2ZXJzaW9uAQ==

And finally, I am proving ownership of the github account by posting this as a gist.

My publicly-auditable identity:

https://keybase.io/anagda

From the command line:

Consider the keybase command line program.

# look me up
keybase id anagda

what?

lol that made my day! I was reading this comment in a wtf moment too.

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