intercept_stream_stream not getting called in grpc.aio

For some reason when using grpc.io with UnaryUnaryClientInterceptor and StreamStreamClientInterceptor, only the UnaryUnaryClientInterceptor gets called, heres the code in async:

import asyncio
import grpc
from grpc.aio import ClientCallDetails, AioRpcError

import geyser_pb2
import geyser_pb2_grpc


class WithHeaders(
    grpc.aio.UnaryUnaryClientInterceptor,
    grpc.aio.StreamStreamClientInterceptor,
):
    def __init__(self, *headers: tuple[str, str]):
        self.headers = headers

    def _insert_headers(self, new_metadata, client_call_details) -> ClientCallDetails:
        metadata = []
        if client_call_details.metadata is not None:
            metadata = list(client_call_details.metadata)
        metadata.extend(new_metadata)

        return ClientCallDetails(
            method=client_call_details.method,
            timeout=client_call_details.timeout,
            metadata=metadata,
            credentials=client_call_details.credentials,
            wait_for_ready=client_call_details.wait_for_ready,
        )

    async def intercept_unary_unary(self, continuation, client_call_details, request):
        print("intercept unary_unary")
        new_client_call_details = self._insert_headers(
            self.headers, client_call_details
        )
        return await continuation(new_client_call_details, request)

    async def intercept_stream_stream(
        self, continuation, client_call_details, request_iterator
    ):
        print("intercept stream_stream")
        new_client_call_details = self._insert_headers(
            self.headers, client_call_details
        )
        return await continuation(new_client_call_details, request_iterator)


class GeyserClient:
    def __init__(self, channel):
        self.stub = geyser_pb2_grpc.GeyserStub(channel)

    async def get_version(self):
        request = geyser_pb2.GetVersionRequest()
        return await self.stub.GetVersion(request)

    async def subscribe_with_request(self, *requests: geyser_pb2.SubscribeRequest):
        async def request_generator():
            for req in requests:
                yield req

        async for response in self.stub.Subscribe(request_generator()):
            yield response


async def main():
    inter_auth = WithHeaders(("something", "12345"))

    async with grpc.aio.secure_channel(
        "...",
        grpc.ssl_channel_credentials(),
        interceptors=[inter_auth],
    ) as channel:
        client = GeyserClient(channel)

        ver = await client.get_version()
        print(f"{ver=}")

        req = geyser_pb2.SubscribeRequest(
            ...
        )

        async for data in client.subscribe_with_request(req):
            print(data)


if __name__ == "__main__":
    asyncio.run(main())

and working code use non-async:

import itertools
import grpc
from grpc import ssl_channel_credentials
from grpc.aio import ClientCallDetails

import geyser_pb2
import geyser_pb2_grpc


class WithHeaders(
    grpc.UnaryUnaryClientInterceptor,
    grpc.StreamStreamClientInterceptor,
):
    def __init__(self, *headers: tuple[str, str]):
        self.headers = headers

    def _insert_headers(self, new_metadata, client_call_details) -> ClientCallDetails:
        metadata = []
        if client_call_details.metadata is not None:
            metadata = list(client_call_details.metadata)
        metadata.extend(new_metadata)

        return ClientCallDetails(
            client_call_details.method,
            client_call_details.timeout,
            metadata,
            client_call_details.credentials,
            False,
        )

    def intercept_unary_unary(self, continuation, client_call_details, request):
        print("intercept unary_unary")
        new_client_call_details = self._insert_headers(
            self.headers, client_call_details
        )
        return continuation(new_client_call_details, request)

    def intercept_stream_stream(
        self,
        continuation,
        client_call_details,
        request_iterator,
    ):
        print("intercept stream_stream")
        new_client_call_details = self._insert_headers(
            self.headers, client_call_details
        )
        return continuation(new_client_call_details, request_iterator)


class GeyserClient:
    def __init__(self, channel):
        self.stub = geyser_pb2_grpc.GeyserStub(channel)

    def get_version(self):
        request = geyser_pb2.GetVersionRequest()
        return self.stub.GetVersion(request)

    def subscribe_with_request(
        self,
        *requests: geyser_pb2.SubscribeRequest,
    ):
        it = iter(requests)
        stream = self.stub.Subscribe(it)
        return stream

def main():
    inter_auth = WithHeaders(("something", "12345"))

    with grpc.secure_channel(
        "...",
        ssl_channel_credentials(),
        options=[("grpc.enable_interceptor", 1)],
    ) as channel:
        channel = grpc.intercept_channel(channel, inter_auth)
        client = GeyserClient(channel)

        ver = client.get_version()
        print(f"{ver=}")

        req = geyser_pb2.SubscribeRequest(
            ...
        )
        for data in client.subscribe_with_request(req):
            print(data)


if __name__ == "__main__":
    main()

GetVersion -> unary_unary
Subscribe -> stream_stream

I tried the code in async (grpc.aio) and sync (grpc): expected intercept_stream_stream to work in both implementations, but didn’t only in sync

New contributor

Martim Martins is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật