nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: google/jax
Pull request #19943 (2024-02-23 08:33:38)

Enhance compilation cache key generation with a custom hook.
Enhance compilation cache key generation with a custom hook.

The custom hook is called every time the cache key is
generated. It can be programmed to add a custom string that
is then hashed as part of the cache key.

Testing: test workloads.


google-cla[bot]: Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this [failed invocation](https://github.com/google/jax/pull/19943/checks?check_run_id=21898846921) of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Patch:
diff --git a/jax/_src/cache_key.py b/jax/_src/cache_key.py
index 43bb0fb06..13f3e2933 100644
--- a/jax/_src/cache_key.py
+++ b/jax/_src/cache_key.py
@@ -51,6 +51,15 @@ def get_flag_prefixes() -> list[str]:
   return _extra_flag_prefixes
 
 
+def custom_hook() -> str:
+  """Custom hook for any addition to the cache key.
+
+  The custom hook will be called everytime get() is called and can be
+  defined to return a string that will be hashed into the cache key.
+  """
+  return ""
+
+
 def get(module: ir.Module,
         devices: np.ndarray,
         compile_options: xla_client.CompileOptions,
@@ -86,6 +95,7 @@ def get(module: ir.Module,
          lambda hash_obj: _hash_accelerator_config(hash_obj, devices, backend)),
       ("compression",
        lambda hash_obj: _hash_string(hash_obj, compression_algorithm)),
+      ("custom_hook", lambda hash_obj: _hash_string(hash_obj, custom_hook())),
   ]
 
   hash_obj = hashlib.sha256()


Test patch:
diff --git a/tests/cache_key_test.py b/tests/cache_key_test.py
index 8d04aeceb..33fc02009 100644
--- a/tests/cache_key_test.py
+++ b/tests/cache_key_test.py
@@ -127,6 +127,21 @@ class CacheKeyTest(jtu.JaxTestCase):
         cache_key.get(computation, devices, compile_options_filled, backend),
     )
 
+  def test_custom_hook(self):
+    computation = jax.jit(lambda x, y: x + y).lower(1, 1).compiler_ir()
+    devices = np.array([[jax.local_devices()[0]]])
+    compile_options = compiler.get_compile_options(
+        num_replicas=1, num_partitions=1
+    )
+    backend = xla_bridge.get_backend()
+    original_custom_hook = cache_key.custom_hook
+    cache_key.custom_hook = lambda: "hook1"
+    key1 = cache_key.get(computation, devices, compile_options, backend)
+    cache_key.custom_hook = lambda: "hook2"
+    key2 = cache_key.get(computation, devices, compile_options, backend)
+    cache_key.custom_hook = original_custom_hook
+    self.assertNotEqual(key1, key2)
+
   def test_different_computations(self):
     computation1 = jax.jit(lambda x, y: x + y).lower(1, 1).compiler_ir()
     computation2 = jax.jit(lambda x, y: x * y).lower(2, 2).compiler_ir()
Repository: GoogleCloudPlatform/cloud-sql-python-connector
Pull request #828 (2023-08-17 20:01:34)

fix: re-use existing connections on force refresh


Patch:
diff --git a/google/cloud/sql/connector/connector.py b/google/cloud/sql/connector/connector.py
index 8bed28c..b0c5fa9 100755
--- a/google/cloud/sql/connector/connector.py
+++ b/google/cloud/sql/connector/connector.py
@@ -280,7 +280,7 @@ class Connector:
 
         except Exception:
             # with any exception, we attempt a force refresh, then throw the error
-            instance.force_refresh()
+            await instance.force_refresh()
             raise
 
     def __enter__(self) -> Any:
diff --git a/google/cloud/sql/connector/instance.py b/google/cloud/sql/connector/instance.py
index b3fc396..4b0814a 100644
--- a/google/cloud/sql/connector/instance.py
+++ b/google/cloud/sql/connector/instance.py
@@ -255,7 +255,7 @@ class Instance:
         self._current = self._schedule_refresh(0)
         self._next = self._current
 
-    def force_refresh(self) -> None:
+    async def force_refresh(self) -> None:
         """
         Forces a new refresh attempt immediately to be used for future connection attempts.
         """
@@ -263,8 +263,9 @@ class Instance:
         if not self._refresh_in_progress.is_set():
             self._next.cancel()
             self._next = self._schedule_refresh(0)
-        # block all sequential connection attempts on the next refresh result
-        self._current = self._next
+        # block all sequential connection attempts on the next refresh result if current is invalid
+        if not await _is_valid(self._current):
+            self._current = self._next
 
     async def _perform_refresh(self) -> InstanceMetadata:
         """Retrieves instance metadata and ephemeral certificate from the


Test patch:
diff --git a/tests/unit/test_instance.py b/tests/unit/test_instance.py
index 0886410..0bd0a11 100644
--- a/tests/unit/test_instance.py
+++ b/tests/unit/test_instance.py
@@ -194,7 +194,7 @@ async def test_force_refresh_cancels_pending_refresh(
     pending_refresh = instance._next
     assert instance._refresh_in_progress.is_set() is False
 
-    instance.force_refresh()
+    await instance.force_refresh()
 
     # pending_refresh has to be awaited for it to raised as cancelled
     with pytest.raises(asyncio.CancelledError):
Repository: GoogleCloudPlatform/cloud-sql-python-connector
Pull request #985 (2024-01-26 18:27:24)

chore: improve connector.close
Attempting to make the `.close` of the Connector more useful and to attempt to close on garbage collection.

Garbage collection does not guarantee when something will be called. `.close()` needs to be able to be called twice in case someone explicitly closes and then garbage collection is performed.


Patch:
diff --git a/google/cloud/sql/connector/connector.py b/google/cloud/sql/connector/connector.py
index 132f385..463b58c 100755
--- a/google/cloud/sql/connector/connector.py
+++ b/google/cloud/sql/connector/connector.py
@@ -308,15 +308,17 @@ class Connector:
 
     def close(self) -> None:
         """Close Connector by stopping tasks and releasing resources."""
-        close_future = asyncio.run_coroutine_threadsafe(
-            self.close_async(), loop=self._loop
-        )
-        # Will attempt to safely shut down tasks for 5s
-        close_future.result(timeout=5)
+        if self._loop.is_running():
+            close_future = asyncio.run_coroutine_threadsafe(
+                self.close_async(), loop=self._loop
+            )
+            # Will attempt to safely shut down tasks for 5s
+            close_future.result(timeout=5)
         # if background thread exists for Connector, clean it up
         if self._thread:
-            # stop event loop running in background thread
-            self._loop.call_soon_threadsafe(self._loop.stop)
+            if self._loop.is_running():
+                # stop event loop running in background thread
+                self._loop.call_soon_threadsafe(self._loop.stop)
             # wait for thread to finish closing (i.e. loop to stop)
             self._thread.join()
 
@@ -327,6 +329,12 @@ class Connector:
             *[instance.close() for instance in self._instances.values()]
         )
 
+    def __del__(self) -> None:
+        """Close Connector as part of garbage collection"""
+        # only want to call destructor when used for sync connections
+        if self._thread:
+            self.close()
+
 
 async def create_async_connector(
     ip_type: IPTypes = IPTypes.PUBLIC,


Test patch:
diff --git a/tests/unit/test_connector.py b/tests/unit/test_connector.py
index cc0623c..0a59be1 100644
--- a/tests/unit/test_connector.py
+++ b/tests/unit/test_connector.py
@@ -137,3 +137,16 @@ def test_Connector_close_kills_thread() -> None:
     connector.close()
     # check that connector thread is no longer running
     assert connector._thread.is_alive() is False
+
+
+def test_Connector_close_called_multiple_times() -> None:
+    """Test that Connector.close can be called multiple times."""
+    # open and close Connector object
+    connector = Connector()
+    # verify background thread exists
+    assert connector._thread
+    connector.close()
+    # check that connector thread is no longer running
+    assert connector._thread.is_alive() is False
+    # call connector.close a second time
+    connector.close()
Repository: mercadona/rele
Pull request #173 (2020-07-08 15:28:27)

Deprecate GC_CREDENTIALS
### :tophat: What?

Deprecate `GC_CREDENTIALS`

### :thinking: Why?

It will be removed in 1.0.0



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 192a567..076cd0a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@ Changelog
 
 `0.13.0` (TBD)
 * Add documentation for class based subscriptions (#169)
+* Deprecate GC_CREDENTIALS setting
 
 `0.13.dev0` (2020-6-16)
 * Traverse all packages to autodiscover all subs.py modules (#167)
diff --git a/rele/config.py b/rele/config.py
index 694a4ff..8bb94d5 100644
--- a/rele/config.py
+++ b/rele/config.py
@@ -1,5 +1,6 @@
 import importlib
 import os
+import warnings
 
 from google.oauth2 import service_account
 
@@ -55,6 +56,11 @@ class Config:
                 self.gc_credentials_path
             )
         elif self._credentials:
+            warnings.warn(
+                "Usage of GC_CREDENTIALS is deprecated and will be removed in an "
+                "upcoming release. Please use GC_CREDENTIALS_PATH.",
+                DeprecationWarning,
+            )
             return self._credentials
 
         else:


Test patch:
diff --git a/tests/test_config.py b/tests/test_config.py
index ec3d8e0..d0fe1ef 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -72,6 +72,16 @@ class TestConfig:
         assert isinstance(config.credentials, service_account.Credentials)
         assert config.middleware == ["rele.contrib.DjangoDBMiddleware"]
 
+    def test_warns_when_gc_credentials_is_passed(self, project_id, credentials):
+        settings = {
+            "GC_PROJECT_ID": project_id,
+            "GC_CREDENTIALS": credentials,
+        }
+        config = Config(settings)
+
+        with pytest.warns(DeprecationWarning):
+            config.credentials
+
     def test_inits_service_account_creds_when_credential_path_given(self, project_id):
         settings = {
             "GC_PROJECT_ID": project_id,
Repository: pires/go-proxyproto
Pull request #41 (2020-10-09 12:02:11)

Add PP2SSL.Marshal
This formats a PP2SSL struct into a TLV.


Patch:
diff --git a/tlvparse/ssl.go b/tlvparse/ssl.go
index 3e68734..ba08af9 100644
--- a/tlvparse/ssl.go
+++ b/tlvparse/ssl.go
@@ -65,6 +65,25 @@ func (s PP2SSL) SSLVersion() (string, bool) {
 	return "", false
 }
 
+// Marshal formats the PP2SSL structure as a TLV.
+func (s PP2SSL) Marshal() (proxyproto.TLV, error) {
+	v := make([]byte, 5)
+	v[0] = s.Client
+	binary.BigEndian.PutUint32(v[1:5], s.Verify)
+
+	tlvs, err := proxyproto.JoinTLVs(s.TLV)
+	if err != nil {
+		return proxyproto.TLV{}, err
+	}
+	v = append(v, tlvs...)
+
+	return proxyproto.TLV{
+		Type:   proxyproto.PP2_TYPE_SSL,
+		Length: len(v),
+		Value:  v,
+	}, nil
+}
+
 // ClientCN returns the string representation (in UTF8) of the Common Name field (OID: 2.5.4.3) of the client
 // certificate's Distinguished Name and whether that extension exists.
 func (s PP2SSL) ClientCN() (string, bool) {


Test patch:
diff --git a/tlvparse/ssl_test.go b/tlvparse/ssl_test.go
index 0ac4f64..0e2ce3b 100644
--- a/tlvparse/ssl_test.go
+++ b/tlvparse/ssl_test.go
@@ -1,6 +1,7 @@
 package tlvparse
 
 import (
+	"reflect"
 	"testing"
 
 	"github.com/pires/go-proxyproto"
@@ -88,3 +89,40 @@ func TestParseV2TLV(t *testing.T) {
 		})
 	}
 }
+
+func TestPP2SSLMarshal(t *testing.T) {
+	ver := "TLSv1.3"
+	cn := "example.org"
+	pp2 := PP2SSL{
+		Client: PP2_BITFIELD_CLIENT_SSL,
+		Verify: 0,
+		TLV: []proxyproto.TLV{
+			{
+				Type:   proxyproto.PP2_SUBTYPE_SSL_VERSION,
+				Length: len(ver),
+				Value:  []byte(ver),
+			},
+			{
+				Type:   proxyproto.PP2_SUBTYPE_SSL_CN,
+				Length: len(cn),
+				Value:  []byte(cn),
+			},
+		},
+	}
+
+	raw := []byte{0x1, 0x0, 0x0, 0x0, 0x0, 0x21, 0x0, 0x7, 0x54, 0x4c, 0x53, 0x76, 0x31, 0x2e, 0x33, 0x22, 0x0, 0xb, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x6f, 0x72, 0x67}
+	want := proxyproto.TLV{
+		Type:   proxyproto.PP2_TYPE_SSL,
+		Length: len(raw),
+		Value:  raw,
+	}
+
+	tlv, err := pp2.Marshal()
+	if err != nil {
+		t.Fatalf("PP2SSL.Marshal() = %v", err)
+	}
+
+	if !reflect.DeepEqual(tlv, want) {
+		t.Errorf("PP2SSL.Marshal() = %#v, want %#v", tlv, want)
+	}
+}
Repository: pires/go-proxyproto
Pull request #55 (2020-11-25 13:46:18)

Add Generic Underlying Connection Getter
After the type-specific underlying connection getters were introduced in #51 There's still use for a generic getter.

For example: there might be more wrappers to the connection, and if all wrappers could implement the `Conn() net.Conn` interface, a trivial resolver could unwrap them all and get to the actual-underlying connection in a generic way.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/35239595/badge)](https://coveralls.io/builds/35239595)

Coverage increased (+0.03%) to 93.514% when pulling **b4b6ec84d5cc9d2b838a5e86d763834ee46d2c29 on guysv:rawmethod** into **9d53ed5686d1944a3e0a53a7521c578a92e37719 on pires:master**.

guysv: Done with @pires requests, (bump, not how GH's PR etiquette is working here).

Patch:
diff --git a/protocol.go b/protocol.go
index 30477bb..9fb2df1 100644
--- a/protocol.go
+++ b/protocol.go
@@ -154,19 +154,36 @@ func (p *Conn) RemoteAddr() net.Addr {
 	return p.header.SourceAddr
 }
 
-// TCPConn returns is the underlying connection as TCPConn
+// Raw returns the underlying connection which can be casted to
+// a concrete type, allowing access to specialized functions.
+//
+// Use this ONLY if you know exactly what you are doing. 
+func (p *Conn) Raw() net.Conn {
+	return p.conn
+}
+
+// TCPConn returns the underlying TCP connection,
+// allowing access to specialized functions.
+//
+// Use this ONLY if you know exactly what you are doing. 
 func (p *Conn) TCPConn() (conn *net.TCPConn, ok bool) {
 	conn, ok = p.conn.(*net.TCPConn)
 	return
 }
 
-// UnixConn returns is the underlying connection as UnixConn
+// UnixConn returns the underlying Unix socket connection,
+// allowing access to specialized functions.
+//
+// Use this ONLY if you know exactly what you are doing. 
 func (p *Conn) UnixConn() (conn *net.UnixConn, ok bool) {
 	conn, ok = p.conn.(*net.UnixConn)
 	return
 }
 
-// UDPConn returns is the underlying connection as UDPConn
+// UDPConn returns the underlying UDP connection,
+// allowing access to specialized functions.
+//
+// Use this ONLY if you know exactly what you are doing. 
 func (p *Conn) UDPConn() (conn *net.UDPConn, ok bool) {
 	conn, ok = p.conn.(*net.UDPConn)
 	return


Test patch:
diff --git a/protocol_test.go b/protocol_test.go
index daadd37..f5c08be 100644
--- a/protocol_test.go
+++ b/protocol_test.go
@@ -519,6 +519,10 @@ func Test_ConnectionCasts(t *testing.T) {
 	if ok {
 		t.Fatal("err: should be a tcp connection not unix")
 	}
+	_, ok = proxyprotoConn.Raw().(*net.TCPConn)
+	if !ok {
+		t.Fatal("err: should be a tcp connection")
+	}
 }
 
 func Test_ConnectionErrorsWhenHeaderValidationFails(t *testing.T) {
Repository: pires/go-proxyproto
Pull request #57 (2020-12-09 10:55:23)

Fixed incorrect truncated tlv error that prevents reading empty (0 byte) TLVs
We hit this issue when reading a 0 byte custom TLV value. Testing identified that this was because the length was 0 bytes and the length check in this PR was affecting it.


Patch:
diff --git a/tlv.go b/tlv.go
index 3bba207..899a220 100644
--- a/tlv.go
+++ b/tlv.go
@@ -55,7 +55,7 @@ func SplitTLVs(raw []byte) ([]TLV, error) {
 		tlv := TLV{
 			Type: PP2Type(raw[i]),
 		}
-		if len(raw)-i <= 3 {
+		if len(raw)-i <= 2 {
 			return nil, ErrTruncatedTLV
 		}
 		tlvLen := int(binary.BigEndian.Uint16(raw[i+1 : i+3])) // Max length = 65K


Test patch:
diff --git a/tlv_test.go b/tlv_test.go
index 04a3fd9..1fad0d9 100644
--- a/tlv_test.go
+++ b/tlv_test.go
@@ -68,6 +68,24 @@ var invalidTLVTests = []struct {
 	},
 }
 
+func TestValid0Length(t *testing.T) {
+	r := bufio.NewReader(bytes.NewReader(append(append(SIGV2, byte(PROXY), byte(TCPv4)), fixtureWithTLV(lengthV4Bytes, fixtureIPv4Address, []byte{byte(PP2_TYPE_MIN_CUSTOM), 0x00, 0x00})...)))
+	h, err := Read(r)
+	if err != nil {
+		t.Fatalf("unexpected error: %v", err)
+	}
+	tlvs, err := h.TLVs()
+	if err != nil {
+		t.Fatalf("unexpected error: %v", err)
+	}
+	if len(tlvs) != 1 {
+		t.Fatalf("expected 1 tlv, got %d", len(tlvs))
+	}
+	if len(tlvs[0].Value) != 0 {
+		t.Fatalf("expected 0 byte tlv value, got %x", tlvs[0].Value)
+	}
+}
+
 func TestInvalidV2TLV(t *testing.T) {
 	for _, tc := range invalidTLVTests {
 		t.Run(tc.name, func(t *testing.T) {
Repository: pires/go-proxyproto
Pull request #67 (2021-02-16 21:32:58)

Add PP2_TYPE_UNIQUE_ID
This PR registers the unique ID TLV which was added to the PROXY spec on March,
5th 2020.



Patch:
diff --git a/tlv.go b/tlv.go
index 899a220..7cc2fb3 100644
--- a/tlv.go
+++ b/tlv.go
@@ -16,6 +16,7 @@ const (
 	PP2_TYPE_AUTHORITY      PP2Type = 0x02
 	PP2_TYPE_CRC32C         PP2Type = 0x03
 	PP2_TYPE_NOOP           PP2Type = 0x04
+	PP2_TYPE_UNIQUE_ID      PP2Type = 0x05
 	PP2_TYPE_SSL            PP2Type = 0x20
 	PP2_SUBTYPE_SSL_VERSION PP2Type = 0x21
 	PP2_SUBTYPE_SSL_CN      PP2Type = 0x22
@@ -97,6 +98,7 @@ func (p PP2Type) Registered() bool {
 		PP2_TYPE_AUTHORITY,
 		PP2_TYPE_CRC32C,
 		PP2_TYPE_NOOP,
+		PP2_TYPE_UNIQUE_ID,
 		PP2_TYPE_SSL,
 		PP2_SUBTYPE_SSL_VERSION,
 		PP2_SUBTYPE_SSL_CN,


Test patch:
diff --git a/tlv_test.go b/tlv_test.go
index 1fad0d9..1f7285c 100644
--- a/tlv_test.go
+++ b/tlv_test.go
@@ -100,7 +100,7 @@ func TestInvalidV2TLV(t *testing.T) {
 
 func TestV2TLVPP2Registered(t *testing.T) {
 	pp2RegTypes := []PP2Type{
-		PP2_TYPE_ALPN, PP2_TYPE_AUTHORITY, PP2_TYPE_CRC32C, PP2_TYPE_NOOP,
+		PP2_TYPE_ALPN, PP2_TYPE_AUTHORITY, PP2_TYPE_CRC32C, PP2_TYPE_NOOP, PP2_TYPE_UNIQUE_ID,
 		PP2_TYPE_SSL, PP2_SUBTYPE_SSL_VERSION, PP2_SUBTYPE_SSL_CN,
 		PP2_SUBTYPE_SSL_CIPHER, PP2_SUBTYPE_SSL_SIG_ALG, PP2_SUBTYPE_SSL_KEY_ALG,
 		PP2_TYPE_NETNS,